我在运行外部进程时,遇到了GUI无法出现并且冻结的问题。在这种情况下,drivers.exe是一个非常简单的程序,用户只需点击“确定”即可退出。因此,每当我点击“确定”时,它就会退出。我试图让我的状态栏在drivers.exe执行时(非常快地)计数。但实际上,在drivers.exe退出之前,我的GUI根本不会出现。
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");
}