当另一个进程退出时在 C# 中引发事件 [英] Raise event in C# when another process exits

查看:34
本文介绍了当另一个进程退出时在 C# 中引发事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道这个问题是否有人问过,但我在上面找不到任何东西,所以如果你能找到一些东西,请引导我走向正确的方向.

I don't know if this question was already asked, but I could not find anything on it, so please lead me in the right direction if you can find something.

基本上,我想向我当前的 C# 程序添加一个事件,以便在另一个指定的进程(example.exe")退出时引发.这可能吗?

Basically, I would like to add an event to my current C# program to be raised when another specified process ("example.exe") exits. Is that possible?

如果这是不可能的,那么是否有一种方法可以在直接路径(C:somefolderpaths...example.exe")退出的指定进程退出时引发事件?

If that is not possible, is there, instead, a way to raise an event when a specified process by direct path ("C:somefolderpaths...example.exe") exits?

补充:我的程序没有启动进程example.exe.

To add: My program does NOT start the process example.exe.

推荐答案

如果您使用的是 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.

这篇关于当另一个进程退出时在 C# 中引发事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆