title: Force kill on process
body: <p>I have this loop that runs continuously as a process to check if the <code>mstsc.exe</code> is running.</p>
  
  <pre><code>for (; ; )
  {
      System.Diagnostics.Process[] pname = System.Diagnostics.Process.GetProcessesByName("mstsc");
  
      if (pname.Length != 0)
      {
  
      }
      else
      {
          System.Diagnostics.Process.Start(@"mstsc.exe");
      }
      System.Threading.Thread.Sleep(3000);
  }
  </code></pre>
  
  <p>The problem is that on logoff, reboot, or shutdown I get this.</p>
  
  <p><img src="https://i.stack.imgur.com/uovvA.png" alt="enter image description here"></p>
  
  <p>I tried to end the process on Form_Closing or with</p>
  
  <pre><code>Microsoft.Win32.SystemEvents.SessionEnded += 
    new Microsoft.Win32.SessionEndedEventHandler(SystemEvents_SessionEnded);
  </code></pre>
  
  <p>and I still get this...</p>
  
  <p>How could I force this process to kill correctly?</p>