Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent windows from going to sleep when my c++ application is running? [duplicate]

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?

like image 477
ProEns08 Avatar asked Jan 17 '16 08:01

ProEns08


1 Answers

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);
like image 179
ddacot Avatar answered Sep 22 '22 18:09

ddacot