Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
960 views
in Technique[技术] by (71.8m points)

wpf - Raise event in C# when another process exits

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.

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?

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.  

  ); 

EDIT

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...