While the methods suggested by others will work, this is not the most efficient way to handle such things. If you keep a loop checking if the process has exited or not, you will spend a lot of system resources. Your concern should be to simply know when the process is ending, and not to sit to check if he has left. So, the right way is to handle events.
The code below explains how to do this using events.
// Declare your process object with WithEvents, so that events can be handled. private Process withEventsField_MyProcess; Process MyProcess { get { return withEventsField_MyProcess; } set { if (withEventsField_MyProcess != null) { withEventsField_MyProcess.Exited -= MyProcess_Exited; } withEventsField_MyProcess = value; if (withEventsField_MyProcess != null) { withEventsField_MyProcess.Exited += MyProcess_Exited; } } } bool MyProcessIsRunning; private void Button1_Click(System.Object sender, System.EventArgs e) { // start the process. this is an example. MyProcess = Process.Start("Notepad.exe"); // enable raising events for the process. MyProcess.EnableRaisingEvents = true; // set the flag to know whether my process is running MyProcessIsRunning = true; } private void MyProcess_Exited(object sender, System.EventArgs e) { // the process has just exited. what do you want to do? MyProcessIsRunning = false; MessageBox.Show("The process has exited!"); }
EDIT: Knowing if a process is running or not should be easy as they start the process somewhere in the code. Thus, you can set the flag there and set the value to false when the process exits. I updated the code above to show how easy it is to set such a flag.
source share