判断exe执行结束,事件监听

[转载]C#调用Exe文件的方法及如何判断程序调用的exe已结束

   

很简单的代码就可以实现C#调用EXE文件,如下:

1、引入using System.Diagnostics;调用代码:Process.Start(exe文件名);

2、直接System.Diagnostics.Process.Start(exe文件名)。

C#如何判断程序调用的exe已结束

二个方法:以运行系统记事本为例

方法一:这种方法会阻塞当前进程,直到运行的外部程序退出
System.Diagnostics.Process exep = System.Diagnostics.Process.Start(@"C:WindowsNotepad.exe");
exep.WaitForExit();//关键,等待外部程序退出后才能往下执行
MessageBox.Show("Notepad.exe运行完毕");

方法二:为外部进程添加一个事件监视器,当退出后,获取通知,这种方法时不会阻塞当前进程,你可以处理其它事情
System.Diagnostics.Process exep = new System.Diagnostics.Process();
exep.StartInfo.FileName = @"C:WindowsNotepad.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运行完毕");
}
原文地址:https://www.cnblogs.com/powerzhang/p/3948593.html