title: C# - Is it possible to catch a <Process>.Exited event fired from the main process?
body: <p>I am writing a C# console application and am trying to check when my main program's process has exited, so that I can do some cleanup before exiting, but the event never seems to fire. Here is the code to set the event handler:</p>
  
  <pre><code>Process process = Process.GetCurrentProcess();
  CloseConsoleHandler close = new CloseConsoleHandler(test);
  process.EnableRaisingEvents = true;
  process.Exited += close.CloseHandler; 
  //I also tried process.Exited += new EventHandler(close.CloseHandler);
  </code></pre>
  
  <p>It just never seems to fire, not when the program ends naturally, not when I click the close button... never. Is this even possible?</p>