Function Reference


_WinAPI_SetThreadExecutionState

Prevents the system from entering sleep or turning off the display while the current application is running

#include <WinAPIProc.au3>
_WinAPI_SetThreadExecutionState ( $iFlags )

Parameters

$iFlags The thread's execution requirements. This parameter can be one or more of the following values.
$ES_AWAYMODE_REQUIRED
$ES_CONTINUOUS
$ES_DISPLAY_REQUIRED
$ES_SYSTEM_REQUIRED
$ES_USER_PRESENT

Return Value

Success: The value is the previous thread execution state.
Failure: 0.

Remarks

The _WinAPI_SetThreadExecutionState() function cannot be used to prevent the user from putting the computer to sleep.
Applications should respect that the user expects a certain behavior when they close the lid on their laptop or press the power button.

This function does not stop the screen saver from executing.
According to Microsoft Docs $ES_USER_PRESENT should not be used, and $ES_AWAYMODE_REQUIRED should be used only when absolutely necessary.
Constants are defined in APIProcConstants.au3

See Also

Search SetThreadExecutionState in MSDN Library.

Example

#include <MsgBoxConstants.au3>
#include <WinAPIProc.au3>

_Example()

Func _Example()
        ; disable ScreenSaver and other windows sleep features
        _WinAPI_SetThreadExecutionState(BitOR($ES_DISPLAY_REQUIRED, $ES_CONTINUOUS, $ES_SYSTEM_REQUIRED, $ES_AWAYMODE_REQUIRED))
        MsgBox($MB_OK, 'Testing', 'Close this tester window when you be sure that your ScreeSaver not runing.')

        ; back to normal windows behavior
        _WinAPI_SetThreadExecutionState($ES_CONTINUOUS)
EndFunc   ;==>_Example