title: NullReferenceException with no stack trace when hooking SetConsoleCtrlHandler
body: <p>Using code to hook the console close event from <a href="https://stackoverflow.com/questions/474679/capture-console-exit-c/474743#474743">this thread</a>, I sometimes get a <code>NullReferenceException</code> with no stacktrace (most of the times I don't). It happens in both release and debug and "break when an exception is thrown" doesn't help (it breaks, but the stack trace is still empty). I never get this exception when I exit my application normally (which is hitting enter and thus releasing a <code>Console.ReadLine</code>). The Application event log has 2 entries:</p>
  
  <blockquote>
    <p>Application: MyApp.exe Framework Version: v4.0.30319 Description:
    The process was terminated due to an unhandled exception. Exception
    Info: System.NullReferenceException Stack:</p>
  </blockquote>
  
  <p>And:</p>
  
  <blockquote>
    <p>Faulting application name: Gateway.exe, version: 1.0.0.0, time stamp:
    0x4e284101 Faulting module name: unknown, version: 0.0.0.0, time
    stamp: 0x00000000 Exception code: 0xc0000005 Fault offset: 0x004d41ce
    Faulting process id: 0xf00 Faulting application start time:
    0x01cc47b827e19a6e Faulting application path:
    C:\dev\MyApp.exe Faulting module path:
    unknown Report Id: 689c1caa-b3ab-11e0-ba1b-00247e777f12</p>
  </blockquote>
  
  <p>Google has revealed some <a href="http://connect.microsoft.com/VisualStudio/feedback/details/524889/debugging-c-console-application-that-handles-console-cancelkeypress-is-broken-in-net-4-0" rel="nofollow noreferrer">bugs</a> and <a href="http://www.zeroc.com/forums/help-center/5249-capture-console-exit-c.html" rel="nofollow noreferrer">issues</a> with <code>SetConsoleCtrlHandler</code>, so I'm wondering if this is a lost battle.</p>