Windows falls asleep while running a lengthy Visual C ++ program Visual Studio

I am using Windows 8.1, Visual Studio 2013, and I have a C ++ project that lasts more than 15 minutes. But the problem is that the windows are falling asleep while I'm still debugging.

I know that this happened because the sleep timeout was exceeded while the program was running (debugging), and I can easily stop it by increasing the sleep timeout or setting the settings to "never" in the Power Pannel Power Settings.

But for this I want to use software or Visual Studio. I want my computer not to sleep in the middle of executing (debugging) a program.

+6
source share
2 answers

At the program entry point, change the settings, restore the settings at the end, when the debugging session ends.

Take this example ....

#include <cstdlib> //include windows.h using namespace std; void KeepMonitorActive() { // Enable away mode and prevent the sleep idle time-out. SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_AWAYMODE_REQUIRED); } void RestoreMonitorSettings() { // Clear EXECUTION_STATE flags to disable away mode and allow the system to idle to sleep normally. SetThreadExecutionState(ES_CONTINUOUS); } int main() { //Add these 2 lines at the entry point in your program KeepMonitorActive(); atexit(RestoreMonitorSettings); //... } 
+2
source

SetThreadExecutionState Function on Windows

+5
source

All Articles