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?

6
  • 1
    I saw this before asking the question. I did not find there what I want.
    – ProEns08
    Jan 17, 2016 at 8:27
  • 1
    In what way does it not answer your question? Jan 17, 2016 at 8:30
  • I don't know where to put the call to the function SetThreadExecutionState in my code. In the beginning of the main() function or in the infinite processing loop?
    – ProEns08
    Jan 17, 2016 at 8:33
  • @ProEns08: Either in the main function (to make it last for your whole app), or in a message handle for when you want to start preventing sleep. Jan 17, 2016 at 8:45
  • @ProEns08: Ignore me. See Alex Halls comment below. Jan 17, 2016 at 8:48

2 Answers 2

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);
7
  • 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, 2016 at 8: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, 2016 at 8:40
  • @AlexHall: thanks for you informations, this is what I want.
    – ProEns08
    Jan 17, 2016 at 9:00
  • @ddcot: thanks for you information, +1:
    – ProEns08
    Jan 17, 2016 at 9:01
  • @ProEns08 I have added an example, check it out.
    – ddacot
    Jan 17, 2016 at 9:03
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.