I would like to be able to watch a process until it is terminated, and once non existent display a message, how could this be achieved?
Asked
Active
Viewed 7,498 times
6
-
1Use the Process.Exited event. – Hans Passant Jul 17 '11 at 13:55
-
Does this answer your question? [Wait until a process ends](https://stackoverflow.com/questions/3147911/wait-until-a-process-ends) – skst May 31 '20 at 18:16
3 Answers
11
Create/Attach to the process and then either use WaitForExit()
to block until it has exited, or use the OnExited
Event if you don't wish your application to block while it's waiting for the app to exit.
I heartily recommend reviewing the documentation for Process
- right here

Olipro
- 3,489
- 19
- 25
9
The .NET Framework has built in support for this. You need to use the Process.Start
method to start the process, and then call the WaitForExit
method, which will block execution of your application until the process you started has finished and closed.
Sample code:
// Start the process.
Process proc = Process.Start("notepad.exe"); // TODO: NEVER hard-code strings!!!
// Wait for the process to end.
proc.WaitForExit();
// Show your message box.
MessageBox.Show("Process finished.");
Related knowledge base article: How to wait for a shelled application to finish using Visual C#

Cody Gray - on strike
- 239,200
- 50
- 490
- 574
-
One can never overstate the virtues of Event handlers - and OnExited is a great option. – Olipro Jul 17 '11 at 12:21
1
I think this is what you want to do:
System.Diagnostics.Process process=new System.Diagnostics.Process();
process.StartInfo.FileName = "process.exe";
process.Start();
process.WaitForExit();
//process ended
MessageBox.Show("Process terminated");

1' OR 1 --
- 1,694
- 1
- 16
- 32