How to prevent windows from closing when starting a C ++ application?

I am developing an application in C ++ with Qt and visual studio 2015.

I want to know how to prevent the application form from filling out when my application is running. My application should always run in the background and respond to the user commanding his voice.

Are there any barriers to switching from standby when the application starts?

+6
source share
2 answers

Function SetThreadExecutionState

Allows a program to inform the system that it is being used, thereby preventing the system from entering sleep mode 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); 
+6
source

The SetThreadExecutionState API document recommended by Martin Bonner and ddacot explained this quite clearly.

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 set state must remain in effect until the next call that uses ES_CONTINUOUS and one of the other status flags is cleared.

  • ES_SYSTEM_REQUIRED forces the system to be operational by resetting the system idle timer.

  • ES_AWAYMODE_REQUIRED makes multimedia applications run in the background so you can make voice calls with your application.

+5
source

All Articles