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 effec
SetThreadExecutionState is only valid for the thread that calls it. If it's called in a worker thread, even with ES_CONTINUOUS, once the worker thread is dead, the setting is not valid anymore and then screen saver will be on again.
Calling this API from a Timer will wake up a worker thread before previous thread is dead and therefore makes it work.
So if you call SetThreadExecutionState in your main thread, like UI thread in client applications, you don't need timer.