Skip to content

Instantly share code, notes, and snippets.

@EBNull
Created March 27, 2013 22:11
Show Gist options
  • Save EBNull/5258552 to your computer and use it in GitHub Desktop.
Save EBNull/5258552 to your computer and use it in GitHub Desktop.
Forces Windows to keep the display on (temporarily disables sleep mode while running). Netflix's silverlight app failed to do this for me :/
import os
import sys
import ctypes
ES_AWAYMODE_REQUIRED = 0x00000040
ES_CONTINUOUS = 0x80000000
ES_DISPLAY_REQUIRED = 0x2 #Forces the display to be on by resetting the display idle timer.
ES_SYSTEM_REQUIRED = 0x1 #Forces the system to be in the working state by resetting the system idle timer.
def not_null(result, func, arguments):
if result == 0:
raise WinError()
SetThreadExecutionState = ctypes.windll.kernel32.SetThreadExecutionState
SetThreadExecutionState.restype = int
SetThreadExecutionState.argtypes = (ctypes.c_uint, )
SetThreadExecutionState.errcheck = not_null
def main(argv):
sys.stdout.write("Keeping display on. Press enter to exit.\n")
SetThreadExecutionState(ES_CONTINUOUS | ES_DISPLAY_REQUIRED)
sys.stdin.read(1)
if __name__ == '__main__':
sys.exit(main(sys.argv) or 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment