SetThreadExecutionState is not working when called from windows service

前端 未结 3 1077
暗喜
暗喜 2021-02-13 20:15

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

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-13 20:17

    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.

提交回复
热议问题