加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

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

发布时间:2020-12-14 01:48:21 所属栏目:Windows 来源:网络整理
导读:我正在使用 Windows 8.1,Visual Studio 2013,我有一个运行超过15分钟的C项目.但问题是当我还在调试时窗口进入睡眠状态. 我知道这是因为在运行程序(调试)时超出了睡眠等待时间,我可以通过增加睡眠等待时间或在Windows控制面板电源设置中将设置设置为“从不”
我正在使用 Windows 8.1,Visual Studio 2013,我有一个运行超过15分钟的C项目.但问题是当我还在调试时窗口进入睡眠状态.

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

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

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

以这个例子为例….

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

   //...
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读