The answer by Mika need clarification and improvements. First of all, see my comments to his answer.
As if you only know the process by its name, there is no reliable way to know that the project found among running processes is the one you need.
I assume you are talking about a process you start as a child process of your current process using
System.Diagnostics.Project.Start
. There are several methods under this name, and all of them allow to check up the status of the process. First of all, you need to get an instance of the
Project
type in question. There are 5 static methods; and each of them returns this instance, and the only non-static (instance) method is called with the instance created with the constructor, presumably with assigned
ProcessInfo
. For this method, you should first check up if the start was successful, by its Boolean return value. So, in all cases, you have an instance of the class
Process
.
See
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx[
^].
Now, you need to check up it's current status. One method to do this is the property
ExitCode
. You need to assume that the process was successfully started (see above) and you use the same instance of
Process
. If you get an exit code value, it means that the process is exited. If it is not, you will get an exception. You cannot get the exception
NotSupportedException
because you know that the process start was successful. When you have the exception
InvalidOperationException
, it cannot be the case when a handle is invalid, by the reason explained above. So, this exception is the indication that the process is running. Catch this exception while checking
ExitCode
and you are done.
See
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exitcode.aspx[
^].
Alternatively, you can check up the property
ExitTime
, see
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exittime.aspx[
^]. You will need to handle exceptions in the same way as in the method based on
ExitCode
.
Finally, handle the event
Exited
to capture the moment of exiting:
myChildProcess.Exited += (sender, eventArgs) => {
this.ChildIsRunning = false;
}
See
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.exited.aspx[
^].
—SA