Windows sleeps while running a long C++ Visual Studio program
Asked Answered
T

2

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.

Timi answered 15/9, 2015 at 4:39 Comment(8)
Call SetThreadExecutionState() periodically.Adenoidectomy
Refer codeguru.com/columns/vb/…Adenoidectomy
But I am just curious, why don't you simply change system settings and prevent sleep?Adenoidectomy
@user1: blogs.msdn.com/b/oldnewthing/archive/2008/12/11/9193695.aspxPoseur
@Adenoidectomy I'm afraid that the link u provided doesn't have the solution for C++Timi
Yes, solution is in C#, I just want to let you know the idea. Take idea from there and implement it on your own. Stackoverflow is not a platform where you get ready-made solutions!Adenoidectomy
@Adenoidectomy Preventing sleep via system settings is not a solution as I would be working mostly on this project for months and so I want sleep option workin in my pcTimi
@Adenoidectomy and thanks for the idea and I would try itTimi
A
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);

   //...
}
Adenoidectomy answered 15/9, 2015 at 5:36 Comment(8)
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::threadAdenoidectomy
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.Poseur
No, my answer is evolving :)Adenoidectomy
#include "windows.h" should be available. U have commented it.Timi
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.Adenoidectomy
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?Timi
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.Adenoidectomy
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.Timi
B
5

There is SetThreadExecutionState function in windows

Baldpate answered 15/9, 2015 at 4:42 Comment(0)
A
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);

   //...
}
Adenoidectomy answered 15/9, 2015 at 5:36 Comment(8)
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::threadAdenoidectomy
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.Poseur
No, my answer is evolving :)Adenoidectomy
#include "windows.h" should be available. U have commented it.Timi
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.Adenoidectomy
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?Timi
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.Adenoidectomy
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.Timi

© 2022 - 2024 — McMap. All rights reserved.