且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

当另一个进程退出时在 C# 中引发事件

更新时间:2023-02-16 13:35:28

如果您使用的是 C# 4.0,您可以执行以下操作:

If you are using C# 4.0 you can do something like:

 Task.Factory.StartNew(() => 
  { 
      var process = Process.Start("process.exe");
      process.WaitForExit();
  }).ContinueWith(

      //THE CODE THAT WILL RUN AFTER PROCESS EXITED.  

  ); 

编辑

如果您不是流程的创建者,您可以使用 Process.GetProcessesByName 函数从已经可用的进程中检索进程.

If you are not creator of a process, you can use Process.GetProcessesByName function to retrive the process from already available ones.

var process = Process.GetProcessesByName("process.exe");

这样你就可以避免阻塞你的主线程,并在外部进程退出时运行你需要的代码.同时,继续做一些更重要的事情.

In this way you can avoid blocking your main thread, and run the code you need at the moment external process exited. Meanwhile, continue do something more important.