Detecting Shutdown, Logoff, Restart, Sleep
Hello,
What i wish to find is a message or the like that I can use to detect between specific 'shutdown' like events, ie: I wish to be able to find the difference between a shutdown, restart, logoff and sleep initiation.
The problem i have found (as displayed below) is using oClosingEvent.CloseReason the return types provide no further information other then the WindowsShutDown constant (the others are obviously irrelevant in this case).
Code:
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim oClosingEvent As System.Windows.Forms.FormClosingEventArgs
oClosingEvent = e
oClosingEvent.CloseReason
End Sub
The alternative i have found (as displayed below) is using the Override wndproc and catching the message WM_ENDSESSION or WM_QUERYENDSESSION however the problem with these is that they only seem to provide information for a logoff .
Thanks in advance.
Code:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Dim oMessage As System.Windows.Forms.Message
oMessage = m
Select Case oMessage.Msg
Case WM_ENDSESSION
If oMessage.LParam = ENDSESSION_LOGOFF Then
'system is logging off
End If
Case WM_QUERYENDSESSION
If oMessage.LParam = ENDSESSION_LOGOFF Then
End If
End Select
MyBase.WndProc(m)
End Sub
Re: Detecting Shutdown, Logoff, Restart, Sleep
Not only that, but apparently WM_QUERYENDSESSION is not sent to applictions in Vista (I googled that, so don't quote me on it).
It seems to me that you have basically solved the problem. Use the CloseReason enumeration to detect primarily why the form is closing, but if the enumeration equals `None`, then use the override on the WndProc to determine if it is caused by a LogOff...
Re: Detecting Shutdown, Logoff, Restart, Sleep
Re: Detecting Shutdown, Logoff, Restart, Sleep
Thanks for the link, i will have a close look at it tonight.
I also considered solving the problem using both those 'functions' however i become limited to knowing only shutdown/logoff I still cannot detect sleep or restart.
Cheers
Re: Detecting Shutdown, Logoff, Restart, Sleep
SystemEvents.SessionSwitch Event
and
SystemEvents.PowerModeChanged Event
Anyhow, SystemEvents seems to be headed in the general direction of, coupled with all the other stuff previously mentioned...
Re: Detecting Shutdown, Logoff, Restart, Sleep
I was working with this very same thing the other day and here is how I accomplished it:
Add system power mode event handlers to the load event on your app:
Code:
'add the handlers for the system standby, resume, and shutdown
AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf PowerModeChanged
AddHandler Microsoft.Win32.SystemEvents.SessionEnding, AddressOf SessionEnding
Then simply set up events to match:
Code:
Private Sub PowerModeChanged(ByVal sender As System.Object, _
ByVal e As Microsoft.Win32.PowerModeChangedEventArgs)
Select Case e.Mode
Case Microsoft.Win32.PowerModes.Resume
'windows is resuming from sleep
Case Microsoft.Win32.PowerModes.Suspend
'goodnite windows
End Select
End Sub
Private Sub SessionEnding(ByVal sender As System.Object, _
ByVal e As Microsoft.Win32.SessionEndingEventArgs)
Select Case e.Reason
Case Microsoft.Win32.SessionEndReasons.Logoff
'logoff
Case Microsoft.Win32.SessionEndReasons.SystemShutdown
'shutdown
End Select
End Sub
Re: Detecting Shutdown, Logoff, Restart, Sleep
Re: Detecting Shutdown, Logoff, Restart, Sleep
Re: Detecting Shutdown, Logoff, Restart, Sleep
Thank you very much, this is definitively the best solution i have seen. It was a massive help.
Max
Re: Detecting Shutdown, Logoff, Restart, Sleep
:thumb::thumb::thumb::thumb::thumb:
Private Sub PowerModeChanged(ByVal sender As System.Object, _
ByVal e As Microsoft.Win32.PowerModeChangedEventArgs)
Select Case e.Mode
Case Microsoft.Win32.PowerModes.Resume
'windows is resuming from sleep
Case Microsoft.Win32.PowerModes.Suspend
'goodnite windows
End Select
End Sub
Private Sub SessionEnding(ByVal sender As System.Object, _
ByVal e As Microsoft.Win32.SessionEndingEventArgs)
Select Case e.Reason
Case Microsoft.Win32.SessionEndReasons.Logoff
'logoff
Case Microsoft.Win32.SessionEndReasons.SystemShutdown
'shutdown
End Select
End Sub
Thanks, That Works Great!
I Did Notice That FAST USER SWITCHING Must Be Disabled For This To Work.