I have a Process
that I want to Kill
; however sometimes this produces the InvalidOperationException
that says that the process has already exited.
To prevent the exception, I tried to check whether the process exited:
if (!p.HasExited)
{
p.Kill();
}
However, I still occasionally see the exception. Since the process is running in parallel with my code, I assume that the problem could be essentially a race condition, i.e. the process terminating inbetween the check and the Kill()
call.
What is the proper way to kill a process without raising the said exception? Of course that I can try-catch around the code in question, but that just doesn't seem like the best way to solve this.