我尝试了这些代码,但不起作用。
我使用的是 Windows 10 和 VS 2019 (VB.NET)。
Imports System.Runtime.InteropServices
Public Class Form1
<FlagsAttribute()> _
Public Enum EXECUTION_STATE As UInteger
ES_SYSTEM_REQUIRED = &H1
ES_DISPLAY_REQUIRED = &H2
ES_CONTINUOUS = &H80000000UI
End Enum
<DllImport("Kernel32.DLL", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function SetThreadExecutionState(ByVal state As EXECUTION_STATE) As EXECUTION_STATE
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED)
End Sub
End Class
它什么也没显示,我的电脑仍在 sleep 。
最佳答案
您还需要指定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.
SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED Or EXECUTION_STATE.ES_CONTINUOUS)
如果您稍后希望撤消此操作并使系统能够再次进入休眠状态,请调用该函数并仅指定 ES_CONTINUOUS
。
关于vb.net - 防止计算机在程序运行时进入休眠/待机/休眠状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57850624/