gpt4 book ai didi

c# - 从 Windows 服务调用时,SetThreadExecutionState 不起作用

转载 作者:可可西里 更新时间:2023-11-01 12:42:42 24 4
gpt4 key购买 nike

我想阻止系统从 Windows 服务进入休眠/休眠状态。我正在调用 SetThreadExecutionState 函数来执行此操作。不过好像没什么效果。我只想知道 SetThreadExecutionState 函数是否适用于 Windows 服务。如果不是,还有什么替代方法。

下面是我正在使用的 C# 代码。我在 Onstart 服务方法上调用它。

[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)
}

最佳答案

Calling SetThreadExecutionState without ES_CONTINUOUS simply resets the idle timer; to keep the display or system in the working state, the thread must call SetThreadExecutionState periodically.

( source )

您需要不时调用此函数。这不是即发即弃。

关于c# - 从 Windows 服务调用时,SetThreadExecutionState 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5870280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com