6

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.

Samitha Chathuranga
  • 1,689
  • 5
  • 30
  • 57

2 Answers2

5

There is SetThreadExecutionState function in windows

Dmitrii Z.
  • 2,287
  • 3
  • 19
  • 29
2

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);

   //...
}
user1
  • 4,031
  • 8
  • 37
  • 66
  • 1
    This is C++11 solution, but you would not have problem running this example since you are already using VS 2013. VS 2013 supports std::thread – user1 Sep 15 '15 at 05:57
  • 1
    In most cases it wouldn't be necessary to spawn a separate thread. There is usually an existing thread (most commonly the main thread) that will be present for the lifetime of the process. – Harry Johnston Sep 15 '15 at 08:05
  • No, my answer is evolving :) – user1 Sep 15 '15 at 09:25
  • #include "windows.h" should be available. U have commented it. – Samitha Chathuranga Sep 15 '15 at 09:29
  • My answer is for your reference. I don't know what program are you running. Win32 console, mfc ? Most likely Windows.h would be included in your project. – user1 Sep 15 '15 at 09:32
  • 1
    This worked. Thanks user1. But one last question.. Why can't we directly call SetThreadExecutionState() function at the beginning of main method and at the end of it, with relavant arguments. ? Why do we use extra function to call them? Is it a coding standard or is there any other use by the way that you have done it? – Samitha Chathuranga Sep 21 '15 at 05:26
  • 1
    Surely, you can replace KeepMonitorActive() call with SetThreadExecutionState(). But not RestoreMonitorSettings(), the reason I used atexit() is because, It will ensure that it will restore the system settings back. If you don't use atexit(), you would then have to call SetThreadExecutionState(ES_CONTINUOUS); in all places where function "returns" conditionally. from maintenance perspective, it is far easier to add these 2 lines at the main() entry than modify all conditional returns in your program. – user1 Sep 21 '15 at 05:33
  • Many Thanks @user1. I was hoping to accept the answer but missed seeing your comment and so was waiting more... Sorry for keeping u waiting until a acceptance request. – Samitha Chathuranga Sep 24 '15 at 07:05