### 先去 CODE
可以通過引入並呼叫 `SetThreadExecutionState` 去達成防止電腦進入休眠及睡眠模式。
```c#
/**
* application main window
*/
public partial class MainWindow : Window {
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
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,
}
/**
* constructor
*/
public MainWindow() {
// initialize component
InitializeComponent();
// set startup event handler
App.Current.Startup += new StartupEventHandler((sender, e) => {
// keep screen awake
SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
});
// set exit event handler
App.Current.Exit += new ExitEventHandler((sender, e) => {
// restore screen state
SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
});
}
}
```