|
-
Dec 25th, 2007, 02:29 AM
#1
Thread Starter
Member
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
-
Dec 25th, 2007, 05:11 AM
#2
Hyperactive Member
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...
-
Dec 25th, 2007, 12:51 PM
#3
Re: Detecting Shutdown, Logoff, Restart, Sleep
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 26th, 2007, 03:42 AM
#4
Thread Starter
Member
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
-
Dec 27th, 2007, 12:56 AM
#5
Hyperactive Member
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...
-
Dec 27th, 2007, 02:29 AM
#6
Addicted Member
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
Jesse Bunch
www.getbunch.com/
If I have helped you, please rate my posts!
Unless otherwise indicated, I am using the following Products:
Visual Studio .NET 2010
.NET Framework 4.0
-
Dec 27th, 2007, 12:59 PM
#7
Re: Detecting Shutdown, Logoff, Restart, Sleep
Very nice
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Dec 27th, 2007, 01:28 PM
#8
Addicted Member
Re: Detecting Shutdown, Logoff, Restart, Sleep
Jesse Bunch
www.getbunch.com/
If I have helped you, please rate my posts!
Unless otherwise indicated, I am using the following Products:
Visual Studio .NET 2010
.NET Framework 4.0
-
Jan 17th, 2008, 03:43 AM
#9
Thread Starter
Member
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
Last edited by maximg; Jan 17th, 2008 at 03:46 AM.
-
May 21st, 2009, 05:13 PM
#10
New Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|