C#
C# / 절전 모드 방지 하기 / setthreadexecutionstate
캬옹냐옹
2021. 9. 3. 11:37
728x90
//절전 모드 방지
[FlagsAttribute]
public enum EXECUTION_STATE : uint
{
ES_AWAYMODE_REQUIRED = 0x00000040,
ES_CONTINUOUS = 0x80000000,
ES_DISPLAY_REQUIRED = 0x00000002,
ES_SYSTEM_REQUIRED = 0x00000001
// Legacy flag, should not be used.
// ES_USER_PRESENT = 0x00000004
}
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern EXECUTION_STATE SetThreadExecutionState(EXECUTION_STATE esFlags);
private void Prevent_Screensaver(bool enable)
{
if (enable) SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS);
else SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS);
}
//절전 모드 방지 END
private void Form1_Shown(object sender, EventArgs e)
{
//절전 모드 방지
Prevent_Screensaver(true);
}
컴퓨터가 절전모드에 들어가면 프로그램이 종료되는 문제가 있어서 이것저것 디버깅을 해도 답이 나오지 않아서
간단하게 컴퓨터를 절전모드에 못들어가게 해버렸다.
.net 5.0 윈폼으로 만든거라서 그런지 이런저런 버그가 많은거 같다.
출처: https://stackoverflow.com/questions/49045701/prevent-screen-from-sleeping-with-c-sharp
728x90