qt防止Windows10自动睡眠模式或关闭显示器

禁用睡眠模式
SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);

#include <winbase.h>
#include <windows.h>

QT不让windows休眠的方法
对于一些Windows应用程序,必须要保证os不能休眠才能有效工作,如迅雷下载软件,如果os进入休眠,则会导致网络不正常,从而导致不能下载东西。那木有没有1种机制,当打开软件的时候,就自动将os设为不休眠状态呢?这里我介绍一种QT应用程序不让windows进入休眠的方法:

使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入睡眠状态或关闭显示器。❞

禁用睡眠模式

SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED | ES_SYSTEM_REQUIRED);

恢复睡眠模式

SetThreadExecutionState(ES_CONTINUOUS);

只需要在QT应用程序入口函数中加入该语句就可以了,这样os就不会进入休眠了,该语句声明在winbase.h中,是windows的api。

屏幕系统休眠

SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED); 

相关

  • 在程序中使用·SetThreadExecutionState·设置了禁用睡眠模式后,程序退出自动恢复睡眠模式。
  • ES_CONTINUOUS:通知系统所设置的状态应保持有效,直到使用ES_CONTINUOUS的下一个调用和其他状态标志之一被清除为止。
  • ES_DISPLAY_REQUIRED:通过重置显示器空闲计时器来强制显示器开启。
  • ES_SYSTEM_REQUIRED:通过重置系统空闲计时器来强制系统进入工作状态。

Enables an application to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running.

Syntax

EXECUTION_STATE WINAPI SetThreadExecutionState( _In_  EXECUTION_STATE esFlags );

Parameters

esFlags [in]

The thread’s execution requirements. This parameter can be one or more of the following values.

ValueMeaning
ES_AWAYMODE_REQUIRED0x00000040Enables away mode. This value must be specified with ES_CONTINUOUS.Away mode should be used only by media-recording and media-distribution applications that must perform critical background processing on desktop computers while the computer appears to be sleeping. See Remarks.Windows Server 2003 and Windows XP:  ES_AWAYMODE_REQUIRED is not supported.
ES_CONTINUOUS0x80000000Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.
ES_DISPLAY_REQUIRED0x00000002Forces the display to be on by resetting the display idle timer.
ES_SYSTEM_REQUIRED0x00000001Forces the system to be in the working state by resetting the system idle timer.
ES_USER_PRESENT0x00000004This value is not supported. If ES_USER_PRESENT is combined with other esFlags values, the call will fail and none of the specified states will be set.Windows Server 2003 and Windows XP:  Informs the system that a user is present and resets the display and system idle timers. ES_USER_PRESENT must be called with ES_CONTINUOUS.

Return Value:

If the function succeeds, the return value is the previous thread execution state.

If the function fails, the return value is NULL.

Examples:

// Television recording is beginning. Enable away mode and prevent
// the sleep idle time-out.
//
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED |ES_AWAYMODE_REQUIRED);

//
// Wait until recording is complete…
//

//
// Clear EXECUTION_STATE flags to disable away mode and allow the system to idle to sleep normally.
//
SetThreadExecutionState(ES_CONTINUOUS);

作者:

喜欢围棋和编程。

 
发布于 分类 编程标签

发表评论

电子邮件地址不会被公开。