我有一个程序可以启动另一个程序(iexplore.exe),然后我的程序应该“不可见”或至少进入背景,并在此asd.exe运行时停留在该位置。这段代码可以正常工作,但是会占用内存,有时在asd.exe不再运行时,它只是隐藏起来。必须有一种更有效的方法来执行此操作=)
所以我的程序代码:
this.Hide();
Process.Start(Path.Combine(Path, "iexplore.exe"));
Process[] Running_ = null;
do
{ // this loops eats memory!
System.Threading.Thread.Sleep(500);
Running_ = null;
Running_ = Process.GetProcessesByName("iexplore");
}while (Running_.Length > 0);
this.Show();
最佳答案
您将要在此处使用Process.Exited事件,防止阻塞UI线程,避免麻烦的线程。像这样:
private void button1_Click(object sender, EventArgs e) {
var prc = new Process();
prc.EnableRaisingEvents = true;
prc.Exited += processExited;
prc.StartInfo = new ProcessStartInfo("notepad.exe");
prc.Start();
this.Hide();
}
private void processExited(object sender, EventArgs e) {
this.BeginInvoke(new Action(() => {
this.Show();
this.BringToFront();
}));
}
关于c# - 如何在特定进程运行时将程序发送到后台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7584932/