我添加EXECUTION_STATE.ES_SYSTEM_REQUIRED
了“通过重置系统空闲计时器强制系统处于工作状态”,并防止系统进入省电状态。我还将 API 调用约定更改为 use EXECUTION_STATE
,将所有内容包装在一个简单的实用程序类中,并带有一些文档。
''' <summary>
''' Simple power manager class that enables applications to inform the system
''' that it is in use, thereby preventing the system from entering the sleeping
''' power state or turning off the display while the application is running.
''' </summary>
Public Class PowerManager
#Region " Private Sub New "
Private Sub New()
'keep compiler from creating default constructor to create utility class
End Sub
#End Region
''' <summary>
''' Enables applications to inform the system that it is in use, thereby preventing the system from entering the sleeping power state or turning off the display while the application is running.
''' </summary>
''' <param name="esFlags">The thread's execution requirements. This parameter can be one or more of the EXECUTION_STATE values.</param>
''' <returns>
''' <para>If the function succeeds, the return value is the previous thread execution state, as a EXECUTION_STATE value.</para>
''' <para>If the function fails, the return value is NULL.</para>
'''</returns>
''' <remarks>
''' <para>This function does not stop the screen saver from executing.</para>
''' <para>http://msdn.microsoft.com/en-us/library/aa373208.aspx</para>
''' </remarks>
Private Declare Function SetThreadExecutionState Lib "kernel32" (ByVal esFlags As EXECUTION_STATE) As EXECUTION_STATE
Public Enum EXECUTION_STATE As Integer
''' <summary>
''' Informs the system that the state being set should remain in effect until the next call that uses ES_CONTINUOUS and one of the other state flags is cleared.
''' </summary>
ES_CONTINUOUS = &H80000000
''' <summary>
''' Forces the display to be on by resetting the display idle timer.
''' </summary>
ES_DISPLAY_REQUIRED = &H2
''' <summary>
''' Forces the system to be in the working state by resetting the system idle timer.
''' </summary>
ES_SYSTEM_REQUIRED = &H1
End Enum
Public Shared Function PowerSaveOff() As EXECUTION_STATE
Return SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED Or EXECUTION_STATE.ES_DISPLAY_REQUIRED Or EXECUTION_STATE.ES_CONTINUOUS)
End Function
Public Shared Function PowerSaveOn() As EXECUTION_STATE
Return SetThreadExecutionState(EXECUTION_STATE.ES_CONTINUOUS)
End Function
End Class
Public Class Form1
Private _cancel As Boolean
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'set system standby to one minute
_cancel = False
PowerManager.PowerSaveOff()
Do Until _cancel
My.Application.DoEvents()
Loop
PowerManager.PowerSaveOn()
'do not forget to restore your power settings
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
_cancel = True
End Sub
End Class