Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VB10

Give your computer sleep apnea - Don't let it go to sleep

4.74/5 (12 votes)
27 Nov 2011CPOL2 min read 38.8K   2.2K  
A utility to prevent workstations from going to sleep (during long running processes).

Introduction

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 third party model runs, and a 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.

VB
' 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
    ' Stay in working state by resetting display idle timer
    ES_SYSTEM_REQUIRED = &H1
    ' Force display on by resetting system idle timer
    ES_DISPLAY_REQUIRED = &H2
    ' Force this state until next ES_CONTINUOUS call
    ' and one of the other flags are cleared
    ES_CONTINUOUS = &H80000000
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).

No_Sleep.JPG

VB
' 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.

No_Sleep_NotifyIcon.JPG

VB
' 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 (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).

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)