V
V
veitmen2015-12-08 10:32:37
.NET
veitmen, 2015-12-08 10:32:37

How to track the moment of going into sleep mode?

Hello.
There is a task to monitor the computer going to sleep. I found this standard solution:

SystemEvents.PowerModeChanged += OnPowerChange;

private void OnPowerChange(object s, PowerModeChangedEventArgs e) 
{
    switch ( e.Mode ) 
    {
        case PowerModes.Resume: 
        break;
        case PowerModes.Suspend:
        break;
    }
}

BUT it works correctly only when the button goes into sleep mode. When we automatically go to sleep, the code is not executed.
Help me find an iron solution that will accurately track the moment of going into sleep mode.
Thank you. :)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melz, 2015-12-08
@veitmen

Have you tried the System.Management.ManagementEventWatcher thing ?
As an alternative, we can offer to lock the user session when going to sleep and catch OnSessionSwitch - SessionSwitchReason.SessionLock.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question