Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SetThreadExecutionState preventing manual sleep

Tags:

windows

winapi

On Windows 7, I'm seeing that a call to SetThreadExecutionState with the parameter ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED is preventing the user from manually putting the system to sleep.

Upon opening the Start menu and selecting 'Sleep,' the display turns off, but my application continues processing. This is evident by the fact that the fan continues at full speed and upon waking the computer, I can see that the operations that were being carried out when the sleep occurred have progressed.

How can I allow the user to manually put their computer to sleep while SetThreadExecutionState is in effect?

like image 444
Collin Dauphinee Avatar asked Jan 31 '14 18:01

Collin Dauphinee


1 Answers

Inclusion of the ES_AWAYMODE_REQUIRED option causes this problem. Quoting from the MSDN article:

The ES_AWAYMODE_REQUIRED value should be used only when absolutely necessary by media applications that require the system to perform background tasks such as recording television content or streaming media to other devices while the system appears to be sleeping. Applications that do not require critical background processing or that run on portable computers should not enable away mode because it prevents the system from conserving power by entering true sleep.

like image 149
Hans Passant Avatar answered Nov 15 '22 05:11

Hans Passant