Click here to Skip to main content
13,053,844 members (55,728 online)
Click here to Skip to main content
Add your own
alternative version


21 bookmarked
Posted 25 Nov 2011

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

, 27 Nov 2011
Rate this:
Please Sign up or sign in to vote.
A utility to prevent workstations from going to sleep (during long running processes).


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.

' 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
    ' Stay in working state by resetting display idle timer
    ' Force display on by resetting system idle timer
    ' 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
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 _
End Function

API execution states (ES_SYSTEM_REQUIRED, ES_DISPLAY_REQUIRED, and ES_CONTINUOUS) are explained at

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
    Thread.Sleep(500)   ' keeps button red for time
    Status_Button.BackColor = Save_Backcolor
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
End Sub

' hide application form
Private Sub MinimizeAppToTray()
    NoSleep_NotifyIcon.Visible = True
    ' Show ballon text for time when app minimized
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


  • Version 1 posted 25 Nov 2011.
  • Version 2 posted 27 Nov 2011 (changed source code zip file name - with same code).


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


About the Author

United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy Vote of 5 Pin
pauljayhoon27-Aug-15 15:20
memberpauljayhoon27-Aug-15 15:20 
QuestionInstalls nicely but doesnt work Pin
RobertGormley29-May-13 9:01
memberRobertGormley29-May-13 9:01 
GeneralMy vote of 5 Pin
Md. Marufuzzaman18-Dec-11 5:46
mvpMd. Marufuzzaman18-Dec-11 5:46 
QuestionInsomnia verse Apnea Pin
not_starman28-Nov-11 10:45
membernot_starman28-Nov-11 10:45 
QuestionMissing zip file? Pin
Ed Korsberg27-Nov-11 11:50
memberEd Korsberg27-Nov-11 11:50 
AnswerRe: Missing zip file? Pin
gndzero27-Nov-11 13:25
membergndzero27-Nov-11 13:25 
GeneralRe: Missing zip file? Pin
Ed Korsberg27-Nov-11 13:42
memberEd Korsberg27-Nov-11 13:42 
GeneralMy vote of 5 Pin
sjcardinale26-Nov-11 6:21
membersjcardinale26-Nov-11 6:21 
QuestionGood stuff Pin
jpmik26-Nov-11 4:10
memberjpmik26-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 27 Nov 2011
Article Copyright 2011 by gndzero
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid