I am using Windows 8.1, Visual Studio 2013 and I have a C++ project which runs over 15 minutes. But the problem is the windows gets into sleep while my is still debugging.
I know this is occured because the sleep wait time is exceeded while running the program (debugging), and I can easily stop this by either increasing sleep wait time or set the settings to "never" sleep in the Windows Control Pannel Power Settings.
But I want a programatical or Visual Studio based solution for this. I want my computer not to sleep in the midst of execution (debugging) of a program.
There is SetThreadExecutionState function in windows
At the program entry point change the settings, restore settings at the end when debug session finishes.
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);
//...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With