13

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?

ProEns08
  • 1,856
  • 2
  • 22
  • 38

2 Answers2

20

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);
ddacot
  • 1,212
  • 3
  • 14
  • 40
  • I don't know where to put the call to this function in my code, in the main() or in the infinite processing loop ? – ProEns08 Jan 17 '16 at 08:31
  • 2
    Any place in your program where the function will be called repeatedly, at intervals shorter than the interval when windows goes to sleep, should work. You could also have a function which is called once, which is an infinite loop that calls SetThreadExecutionState, then waits 45 seconds, starts the loop over (and calls the SetThread~ function again), until the application exits. – Alex Hall Jan 17 '16 at 08:40
  • @AlexHall: thanks for you informations, this is what I want. – ProEns08 Jan 17 '16 at 09:00
  • @ddcot: thanks for you information, +1: – ProEns08 Jan 17 '16 at 09:01
  • @ProEns08 I have added an example, check it out. – ddacot Jan 17 '16 at 09:03
  • It seems to me that NameTooLongException's answer details a pre-existing function that you can simply invoke once at the start of main(), calling the function exactly as spelled out by the code he cites. – Alex Hall Jan 17 '16 at 09:31
  • [MSDN](https://msdn.microsoft.com/en-us/library/windows/desktop/aa373208(v=vs.85).aspx): 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. – diversenok Dec 17 '17 at 20:29
9

The document of API SetThreadExecutionState recommended by Martin Bonner and ddacot explained it pretty clear.

According to your description, you should put the following function in main().

SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED);

According to the document,

  • ES_CONTINUOUS informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.

  • ES_SYSTEM_REQUIRED forces the system to be in the working state by resetting the system idle timer.

  • ES_AWAYMODE_REQUIRED forces media applications to run in the background, so that you can call your application by voice.