Running another process without GUI freezing
Posted
by Adam
on Stack Overflow
See other posts from Stack Overflow
or by Adam
Published on 2010-03-09T02:22:57Z
Indexed on
2010/03/09
2:36 UTC
Read the original article
Hit count: 415
c#
|multithreading
I'm having trouble getting my GUI to appear and not freeze while running (and waiting for) an outside process. In this case, drivers.exe is a very simply program where the user simply clicks "OK". So whenever I click OK, it exits. I am trying to simply make my status strip count numbers up (really fast) as drivers.exe is executing. But in practice, my GUI never appears at all until drivers.exe exits.
private void run_drivers()
{
Console.WriteLine("Start Driver");
int driver_timeout_in_minutes = 20;
System.Diagnostics.Process driverproc = System.Diagnostics.Process.Start(Application.StartupPath + "\\" + "drivers.exe");
driverproc.WaitForExit(driver_timeout_in_minutes * 1000 * 60); //uses milliseconds, we must convert
}
private void Form1_Load(object sender, EventArgs e)
{
ThreadStart worker = new ThreadStart(run_drivers);
Console.WriteLine("Main - Creating worker thread");
toolStripStatusLabel1.Text = "hi";
Thread t = new Thread(worker);
t.IsBackground = true;
t.Start();
Console.WriteLine("Main - Have requested the start of worker thread");
int i = 0;
while (t.IsAlive)
{
i++;
toolStripStatusLabel1.Text = i.ToString();
}
Console.WriteLine("Dead");
}
© Stack Overflow or respective owner