簡體   English   中英

Windows在運行長C ++ Visual Studio程序時會休眠

[英]Windows sleeps while running a long C++ Visual Studio program

我使用的是Windows 8.1,Visual Studio 2013,我有一個運行超過15分鍾的C ++項目。 但問題是當我還在調試時窗口進入睡眠狀態。

我知道這是因為在運行程序(調試)時超出了睡眠等待時間,我可以通過增加睡眠等待時間或在Windows控制面板電源設置中將設置設置為“從不”來輕松停止。

但我想要一個基於編程或Visual Studio的解決方案。 我希望我的計算機不要在程序的執行(調試)中睡覺。

Windows中有SetThreadExecutionState函數

在程序入口點更改設置,在調試會話結束時恢復設置。

以這個例子為例....

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

   //...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM