Results 1 to 10 of 10

Thread: Detecting Shutdown, Logoff, Restart, Sleep

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    43

    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

  2. #2
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    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...

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  4. #4

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    43

    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

  5. #5
    Hyperactive Member
    Join Date
    Feb 2006
    Location
    UK
    Posts
    285

    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...

  6. #6
    Addicted Member
    Join Date
    Apr 2006
    Posts
    137

    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

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  8. #8
    Addicted Member
    Join Date
    Apr 2006
    Posts
    137

    Re: Detecting Shutdown, Logoff, Restart, Sleep

    thanks!
    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

  9. #9

    Thread Starter
    Member
    Join Date
    Apr 2007
    Posts
    43

    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.

  10. #10
    New Member
    Join Date
    May 2009
    Location
    Florida,USA
    Posts
    1

    Thumbs up Re: Detecting Shutdown, Logoff, Restart, Sleep



    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width