I've been wondering about this for a while.
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.
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.
ShutdownEvents 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 " & e.Reason)
Console.ReadKey()
End Sub
Public Shared Sub OnShutdown(ByVal sender As Object, ByVal e As SessionEndedEventArgs)
Console.WriteLine("Shutdown - Reason is " & e.Reason)
Console.ReadKey()
End Sub
End Class
End Module
Any help would be appreciated.
Regards,