Windows在运行长C ++ Visual Studio程序时会休眠

时间:2015-09-15 04:39:28

标签: c++ visual-studio-2013 windows-8.1 visual-studio-debugging sleep-mode

我使用的是Windows 8.1,Visual Studio 2013,我有一个运行超过15分钟的C ++项目。但问题是当我还在调试时窗口进入睡眠状态。

我知道这是因为在运行程序(调试)时超出了睡眠等待时间,我可以通过增加睡眠等待时间或将设置设置为"从不"在Windows控制面板电源设置中休眠。

但我想要一个基于编程或基于Visual Studio的解决方案。我希望我的计算机不要在程序的执行(调试)中睡觉。

2 个答案:

答案 0 :(得分:5)

Windows中有SetThreadExecutionState功能

答案 1 :(得分:2)

在程序入口点更改设置,在调试会话结束时恢复设置。

以此为例....

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

   //...
}