I want prevent system from going to sleep/hibernate from a windows service.
I am calling SetThreadExecutionState
function to do that.
But it seems to have no effect.
I just want to know whether the function SetThreadExecutionState
will for windows services. If not what will be the alternative ways to that.
Below is the C# code i am using. I am calling it on Onstart
method of service.
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint SetThreadExecutionState(EXECUTION_STATE esFlags);
private void KeepAlive()
{
SetThreadExecutionState(EXECUTION_STATE.ES_DISPLAY_REQUIRED | EXECUTION_STATE.ES_SYSTEM_REQUIRED | EXECUTION_STATE.ES_CONTINUOUS)
}