二个方法:以运行系统记事本为例
方法一:这种方法会阻塞当前进程,直到运行的外部程序退出
System.Diagnostics.Process exep = System.Diagnostics.Process.Start(@"C:\Windows\Notepad.exe");
exep.WaitForExit();//关键,等待外部程序退出后才能往下执行
MessageBox.Show("Notepad.exe运行完毕");
方法二:为外部进程添加一个事件监视器,当退出后,获取通知,这种方法时不会阻塞当前进程,你可以处理其它事情
System.Diagnostics.Process exep = new System.Diagnostics.Process();
exep.StartInfo.FileName = @"C:\Windows\Notepad.exe";
exep.EnableRaisingEvents = true;
exep.Exited += new EventHandler(exep_Exited);
exep.Start();
//exep_Exited事件处理代码,这里外部程序退出后激活,可以执行你要的操作
void exep_Exited(object sender, EventArgs e)
{
MessageBox.Show("Notepad.exe运行完毕");
}
来源:http://zhidao.baidu.com/link?url=u6hNAXaLxOYEcBIViascG2fgPRorVYQyN9eULCiVyNCKihlqUGJmCBOZWtp6KS8D71tFaCcrTnjt_xmQqqNNLa
C#如何判断程序调用的exe已结束
最新推荐文章于 2025-09-26 13:39:36 发布
本文介绍了两种在C#中启动并监测外部程序的方法。一种是使用`Process.Start`结合`WaitForExit`来阻塞当前进程直至外部程序完成;另一种是非阻塞方式,通过为外部进程添加事件监听器并在其退出时触发事件。
4398

被折叠的 条评论
为什么被折叠?



