title: C# Crash not caught by Debugger
body: <p>I have a pretty large project that just recently started crashing on exit. I am not the greatest C# coder but everything generally works as intended. In this case however, when I close my console application, it crashes but is not caught by the MSVC Debugger.</p>
  
  <p>1) When I build it in Release and run it, it generally always crashes when I exit.</p>
  
  <p>2) When I build it in Debug and run it, it seems to only crash on exit every 1 out of 3-5 times.</p>
  
  <p>I did manage to get the error though when it didn't exit properly, but unfortunately the Exit Code doesn't match anything I can find online.</p>
  
  <pre><code>The program '[11108] MCDaemon.vshost.exe: Managed (v4.0.30319)' has exited with code -1073741510 (0xc000013a).
  </code></pre>
  
  <p>From what I have read on other people's questions, this is generally caused by Unmanaged Code acting up. Is there any way I can actually find out what the issue is short of a massive amounts of debug lines?</p>
  
  <p><em>EDIT</em></p>
  
  <p>From an answer below, this is the code causing the Exit code, but I don't really see anything issue with it.</p>
  
  <pre><code>public static Boolean Handler(MyWin32.CtrlTypes CtrlType)
  {
      // A switch to handle the event type.
      switch (CtrlType)
      {
          case MyWin32.CtrlTypes.CTRL_C_EVENT:
              Program.TerminateProcess();
              break;
          case MyWin32.CtrlTypes.CTRL_CLOSE_EVENT:
              Program.TerminateProcess();
              break;
      }
  
      return true;
  }
  
  public static void TerminateProcess()
  {
      // Stop the Poll Timer from Running
      PollTimer.Stop();
  
      log.LogMessage("Process is being Shutdown.");
      log.LogMessage("Requesting Process to Stop....");
      SendProcessCmd("stop");
  
      // Wait and make sure it has exited
      Thread.Sleep(5000);
  
      if (!myProcess.HasExited)
      {
          log.LogMessage("My Process did not stop on its own, forcing Process to quit.");
          myProcess.Kill();
      }
  
      log.LogMessage("My Process has been Shutdown.");
  }
  </code></pre>