title: How to handle a C# console application terminating?
body: <p>If I have a console application, is there any way I can handle the following:</p>
  
  <ol>
  <li>Ctrl-C (I know the answer to this. Using Console.TreatControlCAsInput and Console.CancelKeyPress)</li>
  <li>Session termination, such as when someone logs off</li>
  <li>Process exit, such as when someone uses the task manager to close the application.</li>
  </ol>
  
  <p>I know that if I was writing a unix application, I would handle various signals to catch the request to close (SIGTERM from memory), but I also know I need to handle these messages pretty quickly and exit before the system does a kill -9 (SIGKILL).</p>
  
  <p>But for a C# console application, I'm not sure how to do this.</p>