Hibernate / Hibernate and Resume / Wakeup Events in Visual Basic.NET

I have a VB.NET application that communicates with some kind of external server (supports login sessions via Intranet) and I want to listen to Sleep / Hibernate events, so when that happens I want to log out of the existing session system before as the computer goes to sleep while my application runs in the background, but does nothing.

Conversely, when a computer resumes with Hibernate or Woke up from sleep, I want to immediately log into the server.

How can I capture these events and execute my code? I believe this applies to the Win32 API, which I should use in VB.

Thanks.

+7
winapi system-calls
source share
2 answers

If your application uses a window and can read the message loop, you can simply subscribe to Microsoft.Win32.SystemEvents.PowerModeChanged , for example:

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 

Otherwise, if you use the service, for example, you need to create a hidden form as described here (scroll down to "Example 2")

+6
source share

try below:

To restart the computer

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

To turn off the computer

 System.Diagnostics.Process.Start("Shutdown", " -s -t 00") 
0
source share

All Articles