title: How to make sure that the process is still running so that I can kill it?
body: <p>I have a <code>Process</code>  that I want to <code>Kill</code>; however sometimes this produces the <code>InvalidOperationException</code> that says that the process has already exited. </p>
  
  <p>To prevent the exception, I tried to check whether the process exited:</p>
  
  <pre><code>if (!p.HasExited)
  {
      p.Kill();
  }
  </code></pre>
  
  <p>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 <code>Kill()</code> call.</p>
  
  <p>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.</p>