title: How to detect application close when no form is present (console application)
body: <p>I've been wondering about this for a while.</p>
  
  <p>I currently want an application to detect when it is being closed. The tricky thing is, this application is a console one meaning there is no form close event or like-wise.</p>
  
  <p>I had a scout on google and really only came up with the code below which detects only shutting down, logging off and not specifically application closing.</p>
  
  <p>ShutdownEvents Code:</p>
  
  <pre><code>Imports Microsoft.Win32
  Module Module1
  
  Sub Main()
      AddHandler (SystemEvents.SessionEnding), AddressOf ShutDown.OnShuttingdown
      AddHandler (SystemEvents.SessionEnded), AddressOf ShutDown.OnShutdown
      Console.ReadKey()
  End Sub
  
  Public Class ShutDown
      Public Shared Sub OnShuttingdown(ByVal sender As Object, ByVal e As SessionEndingEventArgs)
          e.Cancel = True 'only set if you turn down the shutdown request
          Console.WriteLine("Shutting down - Reason is " &amp; e.Reason)
          Console.ReadKey()
      End Sub
  
      Public Shared Sub OnShutdown(ByVal sender As Object, ByVal e As SessionEndedEventArgs)
          Console.WriteLine("Shutdown - Reason is " &amp; e.Reason)
          Console.ReadKey()
      End Sub
  End Class
  End Module
  </code></pre>
  
  <p>Any help would be appreciated.</p>
  
  <p>Regards,</p>