S
Sebastien Lange
Hi,
Try the code below, and become crazy with me!!! (I have WinXP)
My main window call another process (another window that the user must fill
in) and wait until the process is finished.
I have to deal with thread, otherwise the main window could not be
updated/refreshed.
So when the process is finished, the main window should be activated.
Sometimes it is, sometimes not.
Everyting was working fine, until I set Enabled=false before the call, and
set to true after. (I need it)
So I tried to bring it to the top (not topmost) using SetForegroundWindow
and/or SetWindowPos. If the SetForegroundWindow is called and the main
window was not at foreground, then it highlight the item in the taskbar, but
does not show it!
SetWindowPos with HWND_TOP has not effect!
I even tried Activate()! and it first brings to the top the last window
opened (probably VS.NET in this case), and then yes it brings to the top!
So, that's not good enough.
So I'd like to have the same behavior than without Enabled=false, and then
true.
1) If you close the process (notepad here) and the main window was just
behind, it should be activated, without seing another window appearing (even
1 ms)
2) If the main window was not behind, it should also be activated.
Any idea? Hope I'm clear!
Sebastien
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Diagnostics;
namespace WindowsApplication2
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
public Form1()
{
this.button1 = new System.Windows.Forms.Button();
this.button1.Location = new System.Drawing.Point(10, 10);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 48);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.Controls.Add(this.button1);
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
Process process;
private void button1_Click(object sender, System.EventArgs e)
{
process = Process.Start("notepad.exe");
Thread t = new Thread(new ThreadStart(WaitForExit));
t.IsBackground = true;
t.Name = "WaitForExit";
t.Start();
Enabled = false;
while(t.IsAlive)
{
Thread.Sleep(100);
Application.DoEvents();//Allow the main GUI to be refreshed!!!
}
Enabled = true;
//Activate();
}
private void WaitForExit()
{
while (!process.HasExited)
Thread.Sleep(100);
}
}
}
Try the code below, and become crazy with me!!! (I have WinXP)
My main window call another process (another window that the user must fill
in) and wait until the process is finished.
I have to deal with thread, otherwise the main window could not be
updated/refreshed.
So when the process is finished, the main window should be activated.
Sometimes it is, sometimes not.
Everyting was working fine, until I set Enabled=false before the call, and
set to true after. (I need it)
So I tried to bring it to the top (not topmost) using SetForegroundWindow
and/or SetWindowPos. If the SetForegroundWindow is called and the main
window was not at foreground, then it highlight the item in the taskbar, but
does not show it!
SetWindowPos with HWND_TOP has not effect!
I even tried Activate()! and it first brings to the top the last window
opened (probably VS.NET in this case), and then yes it brings to the top!
So, that's not good enough.
So I'd like to have the same behavior than without Enabled=false, and then
true.
1) If you close the process (notepad here) and the main window was just
behind, it should be activated, without seing another window appearing (even
1 ms)
2) If the main window was not behind, it should also be activated.
Any idea? Hope I'm clear!
Sebastien
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Threading;
using System.Diagnostics;
namespace WindowsApplication2
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
public Form1()
{
this.button1 = new System.Windows.Forms.Button();
this.button1.Location = new System.Drawing.Point(10, 10);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(72, 48);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.Controls.Add(this.button1);
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
}
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
Process process;
private void button1_Click(object sender, System.EventArgs e)
{
process = Process.Start("notepad.exe");
Thread t = new Thread(new ThreadStart(WaitForExit));
t.IsBackground = true;
t.Name = "WaitForExit";
t.Start();
Enabled = false;
while(t.IsAlive)
{
Thread.Sleep(100);
Application.DoEvents();//Allow the main GUI to be refreshed!!!
}
Enabled = true;
//Activate();
}
private void WaitForExit()
{
while (!process.HasExited)
Thread.Sleep(100);
}
}
}