我有一个可以播放某种媒体的应用程序,我不希望计算机在我的应用程序运行时休眠。我四处搜寻,才知道这可以通过 P/Invoke 来完成。
显示器也不应关闭,计算机也不应进入睡眠状态。所以,我做了以下测试:
b.Click += (x, y) =>
{
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS | EXECUTION_STATE.ES_AWAYMODE_REQUIRED);
Debug.WriteLine("Power line executed");
};
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
[FlagsAttribute]
public enum EXECUTION_STATE : uint
{
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_SYSTEM_REQUIRED = 0x00000001
}
但是,电脑在一段时间后仍然进入睡眠状态。这里缺少什么?