应用程序在计算机上运行的时间更长,但如果计算机进入睡眠模式,则会出现问题。有没有办法阻止睡眠模式?
答案 0 :(得分:2)
定义一个这样的类
internal static class NativeMethods
{
// Import SetThreadExecutionState Win32 API and necessary flags
[DllImport("kernel32.dll")]
public static extern uint SetThreadExecutionState(uint esFlags);
public const uint ES_CONTINUOUS = 0x80000000;
public const uint ES_SYSTEM_REQUIRED = 0x00000001;
}
把它放到main方法(确保它在application.run调用之前)
// Set new state to prevent system sleep (note: still allows screen saver)
var previousExecutionState = NativeMethods.SetThreadExecutionState(
NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED);