Get Sleep/Hibernate and Resume/Wakeup events in Visual Basic.NET

后端 未结 2 1895
心在旅途
心在旅途 2021-02-09 11:11

I have VB.NET app that communicates with some external server (maintains login sessions via Intranet), and I want to listen for Sleep/Hibernate events such that when it happens,

2条回答
  •  有刺的猬
    2021-02-09 11:43

    If your application is using a window and can read the message loop you can simply subscribe to Microsoft.Win32.SystemEvents.PowerModeChanged like:

    AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf SystemEvents_PowerModeChanged
    
    Private Sub SystemEvents_PowerModeChanged(ByVal sender As Object, ByVal e As PowerModeChangedEventArgs)
    
        Select Case e.Mode
            Case PowerModes.Resume
            Case PowerModes.StatusChange
            Case PowerModes.Suspend
        End Select
    
    End Sub
    

    Else, if you are using a service for example, you have to create a hidden form like described here (scroll down to 'Example 2')

提交回复
热议问题