Give your computer Sleep Apnea - Don't let it go to sleep
Sleep apnea is not normally a good thing, but I needed to prevent our workstations from going to sleep. Our domain workstations were going to sleep in the middle of 3rd party model runs, and group policy overrode any local power settings.
What problem does this solution solve?
This Visual Basic (2010) utility uses the SetThreadExecutionState API to inform the system that it is in use, thereby preventing the system from entering sleep or turning off the display while the application is running. The system automatically detects activities such as local keyboard or mouse input, server activity, and changing window focus (which all will keep a system from going to sleep). But disk, CPU activity and video display are activities that are not automatically detected (and a long running process is not enough to keep a system awake).
How does the code work?
The application calls the SetThreadExecutionState API once when the form is loaded. The system will not sleep (or turn off the display) as long as this application is running.
' API call to prevent sleep (until the application exits)
Private Declare Function uses the SetThreadExecutionState Lib "kernel32" (ByVal esflags As EXECUTION_STATE) As EXECUTION_STATE
' Define the API execution states
Private Enum EXECUTION_STATE
ES_SYSTEM_REQUIRED = &H1 ' Stay in working state by resetting display idle timer
ES_DISPLAY_REQUIRED = &H2 ' Force display on by resetting system idle timer
ES_CONTINUOUS = &H80000000 ' Force this state until next ES_CONTINUOUS call and one of the other flags are cleared
End Enum
' Prevents sleep as form loads
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
No_Sleep()
End Sub
' Call API - force no sleep and no display turn off
Private Function No_Sleep() As EXECUTION_STATE
Return SetThreadExecutionState(EXECUTION_STATE.ES_SYSTEM_REQUIRED Or EXECUTION_STATE.ES_CONTINUOUS Or EXECUTION_STATE.ES_DISPLAY_REQUIRED)
End Function
API execution states (ES_SYSTEM_REQUIRED, ES_DISPLAY_REQUIRED, and ES_CONTINUOUS) are explained at http://msdn.microsoft.com/en-us/library/windows/desktop/aa373208%28v=vs.85%29.aspx
A button on the form flashes red every five seconds (users like to know something is happening)
' Flash button (so users know it's still running)
Private Sub NoSleep_Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NoSleep_Timer.Tick
Dim Save_Backcolor As Color = Status_Button.BackColor
Status_Button.BackColor = Color.Red
Me.Update()
Thread.Sleep(500) ' keeps button red for time
Status_Button.BackColor = Save_Backcolor
Me.Update()
End Sub
Users can double click on the application to minimize the application to the system tray. A balloon tip is displayed when it's minimized so users know which icon is used.
' Minimize to tray if form double clicked
Private Sub Form1_DoubleClick(sender As Object, e As System.EventArgs) Handles Me.DoubleClick
MinimizeAppToTray()
End Sub
' hide application form
Private Sub MinimizeAppToTray()
Me.Hide()
NoSleep_NotifyIcon.Visible = True
' Show ballon text for time when app minimized
NoSleep_NotifyIcon.ShowBalloonTip(16000)
End Sub
Users can double click on the system tray icon to show the application window again, or right click on the icon to close the application (see full source code for these functions).
Points of Interest
This was tested and works on Windows XP, (and both 32/64 bit) Vista and Windows 7 systems.
Here are the check sums of the no_sleep.exe compiled application (VERIFY FILE INTEGRITY PRIOR TO EXECUTING, or recompile to be on the safe side).
MD5 SHA-1
-------------------------------- ----------------------------------------
8397eb0bd800b2ed16dce95c92d2646c 0e679fcb8ef3e34f7528ecb72f963b7d33810a37
History
Version 1 posted 25 Nov 2011
Version 2 posted 27 Nov 2011 (changed source code zip file name - with same code)
发表评论
CFMU4Y I value the article post.Really thank you! Keep writing.
uXWfHU Thanks so much for the article.Really looking forward to read more. Cool.
-1)); waitfor delay '0:0:3' --
-1' OR 2+51-51-1=0+0+0+1 --
329xTF I really like and appreciate your article.Really looking forward to read more. Much obliged.