SetThreadExecutionState without ES_CONTINUOUS doesn't work properly on Windows 11

Oliver Collyer 1 Reputation point
2022-09-08T13:00:25.21+00:00

Here is what is happening. This is Windows 11 by the way, a C++ program using the Win32 API.

  1. Power settings set to sleep in 5 minutes
  2. Code calls SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_CONTINUOUS)
  3. Wait 5 minutes or more
  4. Code calls SetThreadExecutionState(ES_CONTINOUS)
  5. Computer sleeps immediately.

At step 5, I want it to stay awake for another 5 minutes, not sleep as soon as my code has done step 4.

I tried calling SetThreadExecutionState(SYSTEM_REQUIRED) both before and after step 5 but I cannot prevent it sleeping immediately.

This seems wrong; if I've kept the computer awake for X amount of time, I would expect the system idle timer to be reset when I come out of this phase, but I can't see how to achieve this.

Elsewhere (stack overflow) I saw someone mention that SetThreadExecutionState(SYSTEM_REQUIRED) simply doesn't work on Windows 11. Is this true?

https://stackoverflow.com/questions/72436579/setthreadexecutionstatees-system-required-does-not-prevent-system-sleep-on-win

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,422 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,528 questions
Windows 11
Windows 11
A Microsoft operating system designed for productivity, creativity, and ease of use.
8,170 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Junjie Zhu - MSFT 14,831 Reputation points Microsoft Vendor
    2022-09-09T06:37:58.627+00:00

    Hello @Oliver Collyer ,
    Welcome to Microsoft Q&A!

    SetThreadExecutionState(SYSTEM_REQUIRED) simply doesn't work on Windows 11. Is this true?

    This is true, I reproduced the same problem on win11, I will submit this bug through internal channels.
    You can also submit feedback in the API documentation.

    239314-image.png

    Thank you.
    Junjie


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Junjie Zhu - MSFT 14,831 Reputation points Microsoft Vendor
    2022-12-05T07:48:16.487+00:00

    Hello @Oliver Collyer ,

    This is by design. ES_CONTINOUS is implemented as power requests. Prior to Windows 11 power requests were held an additional 2 minutes after they were dropped by the application. This caused issues as some applications would periodically take a power request for a short period keep systems out of sleep indefinitely. This behavior was removed in Windows 11 and now systems are eligible to sleep as soon as the last power request is dropped.

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments