pythonwinapipywin32

How to make SendMessage unblocking?


I have this script to turn the monitors off:

import win32api, win32con

print "start"
win32api.SendMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
print "end"

"end" isn't printed and the script never ends.
It's annoying when the terminal windows stays open.

How do I make SendMessage non-blocking?


Solution

  • You can't make win32api.SendMessage() non-blocking because the underlying Windows function is blocking. Instead you can use win32api.PostMessage(), it has the same signature:

    import win32api, win32con
    
    print "start"
    win32api.PostMessage(win32con.HWND_BROADCAST, win32con.WM_SYSCOMMAND, win32con.SC_MONITORPOWER, 2)
    print "end"