下面的代码允许我防止 Windows 进入空闲状态和我的机器锁定自己:
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
public static void PreventSleep()
{
if (SleepUtil.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS
| EXECUTION_STATE.ES_DISPLAY_REQUIRED
| EXECUTION_STATE.ES_SYSTEM_REQUIRED
| EXECUTION_STATE.ES_AWAYMODE_REQUIRED) == 0)
SleepUtil.SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS
| EXECUTION_STATE.ES_DISPLAY_REQUIRED
| EXECUTION_STATE.ES_SYSTEM_REQUIRED);
}
这很好用,但是我需要做什么才能重新启用空闲?我一直在搞砸一些事情,但每次尝试时,我都必须让笔记本电脑离开 10 分钟,看看它是否空闲(我的电源选项被组策略锁定)。