在另一个进程退出时

在另一个进程退出时

本文介绍了在另一个进程退出时,在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.

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

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.

  );

编辑

如果您不是进程的创建者,则可以使用函数从已经可用的方法中检索过程。

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#中提升事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 05:22