How to prevent Windows from falling asleep when my c ++ application is running?

advertisements

This question already has an answer here:

  • Prevent windows from going into sleep when my program is running? 5 answers
  • Is there a way in Qt to prohibit the computer from going to sleep? 3 answers

I develop an application in c++ with Qt and visual studio 2015.

I want to know how to prevent the application form going to sleep when my application is running. My application should be always running in background and responding to the user commanding it by voice.

Is there any to prevent windows from going to sleep when my application is running?


SetThreadExecutionState function

Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.

Read more about the API here: SetThreadExecutionState

Example:

// The following sets the appropriate flags to prevent system to go into sleep mode.
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);

// This clears the flags and allows the system to sleep normally.
SetThreadExecutionState(ES_CONTINUOUS);