title: Capture console exit C#
body: <p>I have a console application that contains quite a lot of threads.  There are threads that monitor certain conditions and terminate the program if they are true.  This termination can happen at any time.</p>
  
  <p>I need an event that can be triggered when the program is closing so that I can cleanup all of the other threads and close all file handles and connections properly.  I'm not sure if there is one already built into the .NET framework, so I'm asking before I write my own.</p>
  
  <p>I was wondering if there was an event along the lines of:</p>
  
  <pre><code>MyConsoleProgram.OnExit += CleanupBeforeExit;
  </code></pre>