I use Process.Kill() to kill a process. Like this:
if( !process.WaitForExit( 5000 ) ) {
process.Kill();
}
and sometimes the process will exit right in between the lines, so control will get inside if and then Kill will yield an exception:
System.InvalidOperationException
Cannot process request because the process (ProcessIdHere) has exited.
at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.Kill()
//my code here
Now wrapping the code into try-catch doesn't seem to be a good idea because InvalidOperationException can be called for other reasons.
Is there a way to kill a process without getting an exception in the described scenario?