Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check the reason the computer resumed from hibernation

I am creating an application that puts the computer in hibernation for a few hours or even a full week (Using a WaitableTimer and WaitForSingleObject).

There are three reasons why the computer could wake-up

  1. The waitable timer expired and the computer resumes as scheduled.
  2. The user presses the power button which resumes the computer.
  3. The computer turns on unexpectedly.

The first two reasons are perfectly acceptable, they are what the system is designed for. The third reason is of course not so nice.

I would like to be able to differentiate between these three reasons for turning on. Is there any way to do this? It seems here that in the first scenario the WaitForSingleObject method should return WAIT_OBJECT_0 (source). If this is not the case it is either scenario 2 or 3 but I'm not sure how to differentiate between the two. Is there an API to check the reason for resuming from standby?

Another (better) option is to prohibit other devices/software to wake up the computer (e.g. eliminating scenario 3). I have disallowed wake-up capabilities of all devices I see when I run powercfg -devicequery -wake_armed (when I run the command now the it returns NONE). Is there a similar way to see all (active) software that has scheduled the computer to wake up?

(If its any help the computer this software is designed for is a surface 3 pro, with the included (and updated) Windows 8.1 OS installed)

like image 796
Roy T. Avatar asked Jan 23 '15 09:01

Roy T.


1 Answers

1/ You can query the Windows Event Log to see what caused the computer to wake up:

  var log = new EventLog("System");
  var wakeUpEntry = (from entry in log.Entries.Cast<EventLogEntry>()
    where entry.Source == "Microsoft-Windows-Power-Troubleshooter"
          && entry.InstanceId == 1
    orderby entry.TimeWritten descending
    select entry).First();

  Console.WriteLine("{0}", wakeUpEntry.Message);

The message has a "Wake Source" entry ("4USB Root Hub" for instance) that you can parse if you really want to do it programmatically.

2/ You can check if any Windows or external program has set a wake timer on the system with this command:

powercfg -waketimers

If neither command (yours nor this one) shows anything, most of the possible causes have been eliminated.

3/ You can even disable wake timers completely on this machine. In Windows Power Options, Plan Settings, Advanced Plan Settings, Disable Allow wake timers under the Sleep section. I'm not sure if Windows Scheduled Maintenance is affected by this, but you can also turn it off, of course.

4/ Another safety: make sure no devices in the BIOS settings are allowed to wake the PC up, whenever it's configurable.

like image 190
Patrice Gahide Avatar answered Oct 17 '22 14:10

Patrice Gahide