通过SetThreadExecutionState API实现阻止系统休眠。
<span style="font-size:14px;">[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(ExecutionFlag flag);
public enum ExecutionFlag : uint
{
System = 0x00000001,
Display = 0x00000002,
Continus = 0x80000000
}</span>- 只使用Continus参数时,则是回复系统休眠策略。
- 不使用Continus参数时,实现阻止系统休眠或显示器关闭一次。
- 组合使用Continus参数时,实现阻止系统休眠或显示器关闭至线程终止。
<span style="font-size:14px;">private void PreventSleep()
{
SetThreadExecutionState(ExecutionFlag.System |
ExecutionFlag.Display | ExecutionFlag.Continus);
}
private void RestoreSleep()
{
SetThreadExecutionState(ExecutionFlag.Continus);
}</span>
本文介绍如何使用SetThreadExecutionState API来防止系统进入休眠状态或关闭显示器。通过枚举ExecutionFlag并结合C#代码示例,展示了不同参数组合下如何控制系统的睡眠行为。

被折叠的 条评论
为什么被折叠?



