问题
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
- The waitable timer expired and the computer resumes as scheduled.
- The user presses the power button which resumes the computer.
- 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)
回答1:
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.
回答2:
After following the instructions in this article (particularly seeing what last woke it up with powercfg -lastwake
) I found a mention (in this linked article) that:
Windows 10 has a new “feature” that wakes the computer up for what they call “important wake timers”, and you will want to disable that if your computer is waking up all the time. Head into Power Options just like we show above, and then find Sleep -> Allow wake timers -> Plugged in and change the setting from Important Wake Timers Only to Disable.
(emphasis mine)
Pretty sure that's the culprit that wakes my computer from hibernation, but haven't confirmed.
来源:https://stackoverflow.com/questions/28106937/check-the-reason-the-computer-resumed-from-hibernation