Click here to Skip to main content
15,867,488 members
Articles / Programming Languages / Visual Basic 10

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

Rate me:
Please Sign up or sign in to vote.
4.74/5 (12 votes)
27 Nov 2011CPOL2 min read 37.2K   2.1K   23   11
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionAny way to stop it programatically? Pin
corlleonedonss31-Oct-17 0:23
corlleonedonss31-Oct-17 0:23 
AnswerRe: Any way to stop it programatically? Pin
corlleonedonss31-Oct-17 0:51
corlleonedonss31-Oct-17 0:51 
GeneralMy Vote of 5 Pin
pauljayhoon27-Aug-15 15:20
pauljayhoon27-Aug-15 15:20 
QuestionInstalls nicely but doesnt work Pin
RobertGormley29-May-13 9:01
RobertGormley29-May-13 9:01 
GeneralMy vote of 5 Pin
Md. Marufuzzaman18-Dec-11 5:46
professionalMd. Marufuzzaman18-Dec-11 5:46 
QuestionInsomnia verse Apnea Pin
not_starman28-Nov-11 10:45
not_starman28-Nov-11 10:45 
QuestionMissing zip file? Pin
Ed Korsberg27-Nov-11 11:50
Ed Korsberg27-Nov-11 11:50 
AnswerRe: Missing zip file? Pin
gndzero27-Nov-11 13:25
gndzero27-Nov-11 13:25 
Renamed and re-posted the source code zip file (h t t p://www.codeproject.com/KB/vb/No_Sleep/Prevent_Sleep.zip). Contains the same code, only the name was changed (sort of like in Dragnet).
GeneralRe: Missing zip file? Pin
Ed Korsberg27-Nov-11 13:42
Ed Korsberg27-Nov-11 13:42 
GeneralMy vote of 5 Pin
sjcardinale26-Nov-11 6:21
sjcardinale26-Nov-11 6:21 
QuestionGood stuff Pin
jpmik26-Nov-11 4:10
jpmik26-Nov-11 4:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.