Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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, I want to logout an existing session system before computer goes to sleep, while my app will remain running in the background but won't do anything.

And vice versa, when computer is resumed from Hibernate or Woke up from sleep, I want to immediately login to the server.

How can I grab those events and execute my code? I believe this relates to Win32 API which I'm supposed to use in VB.

Thanks.

like image 562
Kushal Avatar asked Jul 19 '12 18:07

Kushal


2 Answers

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')

like image 195
nik Avatar answered Oct 20 '22 15:10

nik


try below:

To restart the pc

System.Diagnostics.Process.Start("Shutdown", " -r -t 00")

To shutdown the pc

System.Diagnostics.Process.Start("Shutdown", " -s -t 00")
like image 27
joey Avatar answered Oct 20 '22 16:10

joey