Search the Community
Showing results for tags 'workstation lock'.
Screensaver, Sleep, Workstation Lock, and Power-Save Disabling Since I see this question asked again and again, and the simple answer isn't always given (or at least, only half of it is), I'm posting this for reference. To disable Power-saving, Workstation Locking, Screensavers, etc., all that's needed is a call to SetThreadExecutionState. No need for timers, nor for emulating mouse or keyboard input. Just make a call to that API once to disable any locking/sleeping/screensaverin'. When you're done, make another call to it with the proper parameters (this part is important), and everything will be restored. NOTE: The 'execution state' should really only matter while the program that made the call is running (its supposed to be per-application). Once it is terminated, the execution state should be restored. However, there have been some unusual reports regarding this, especially when it is called by more than one process. The main functions in my module are _PowerKeepAlive() and _PowerResetState(). One keeps everything 'awake', the other reenables the default state of Windows power settings (including screensavers and workstation locking). The primary reason I've used this myself is for games that forget to call that API function, and after playing with the joystick for a while, a screensaver or lock-screen will pop up. Using these functions will workaround that problem. Also!: If you want to save and restore the current power-savings 'execution state', just pass the return value from _PowerKeepAlive() as the first argument to 'SetThreadExecutionState'. Anyway, here's the main module I use (example use is below): #include-once ; =============================================================================================================================== ; <_PowerKeepAlive.au3> ; ; Functions to prevent/disable sleep/power-savings modes (AND screensaver) ; ; Functions: ; _PowerKeepAlive() ; _PowerResetState() ; ; See also: ; <_ScreenSaverFunctions.au3> ; query, change, enable & disable screensaver. ; ; Author: Ascend4nt ; =============================================================================================================================== ; ========================================================================================================================== ; Func _PowerKeepAlive() ; ; Function to Prevent the Screensaver and Sleep/Power-savings modes from kicking in. ; NOTE: Be sure to reset this state on exit! ; ; Returns: ; Success: @error=0 & previous state as # (typically 0x80000000 [-2147483648]) ; Failure: @error set (returns 0x80000000, but thats just the normal state) ; @error = 2 = DLLCall error. @extended = DLLCall error code (see AutoIt Help) ; ; Author: Ascend4nt ; ========================================================================================================================== Func _PowerKeepAlive() #cs ; Flags: ; ES_SYSTEM_REQUIRED (0x01) -> Resets system Idle timer ; ES_DISPLAY_REQUIRED (0x02) -> Resets display Idle timer ; ES_CONTINUOUS (0x80000000) -> Forces 'continuous mode' -> the above 2 will not need to continuously be reset #ce Local $aRet=DllCall('kernel32.dll','long','SetThreadExecutionState','long',0x80000003) If @error Then Return SetError(2,@error,0x80000000) Return $aRet ; Previous state (typically 0x80000000 [-2147483648]) EndFunc ; ========================================================================================================================== ; Func _PowerResetState() ; ; Function to Reset the Screensaver and Sleep/Power-savings modes to defaults. ; NOTE: The timer is reset on each call to this! ; ; Returns: ; Success: @error=0 & previous state as # ; Failure: @error set (returns 0x80000000, but thats just the normal state) ; @error = 2 = DLLCall error. @extended = DLLCall error code (see AutoIt Help) ; ; Author: Ascend4nt ; ========================================================================================================================== Func _PowerResetState() ; Flag: ES_CONTINUOUS (0x80000000) -> (default) -> used alone, it resets timers & allows regular sleep/power-savings mode Local $aRet=DllCall('kernel32.dll','long','SetThreadExecutionState','long',0x80000000) If @error Then Return SetError(2,@error,0x80000000) Return $aRet ; Previous state EndFunc Example usage: #NoTrayIcon #include "_PowerKeepAlive.au3" ; Singleton code: If WinExists("SA_0bc53fe0-59c2-11e2-bcfd-0800200c9a66_SA") Then Exit AutoItWinSetTitle("SA_0bc53fe0-59c2-11e2-bcfd-0800200c9a66_SA") Opt("TrayOnEventMode", 0) Opt("TrayMenuMode", 1+2) TraySetClick(8+1) Local $iTrayExit = TrayCreateItem("Exit + Reenable Sleep") ; Disable screensaver, power-save, etc _PowerKeepAlive() ; Be sure to register this to reenable power-saving, screensaver, etc OnAutoItExitRegister("_PowerResetState") ; Now we're ready to accept messages TraySetState() While TrayGetMsg() <> $iTrayExit ; No need for sleep WEnd _PowerKeepAlive.au3