views:

67

answers:

1

I have a python script that is running in the background with pythonw. If I close my laptop, it goes into sleep mode. and when I open my laptop, my program has little functionality and freezes after a couple of seconds. Is there any way that my script can tell if my computer is going into sleep mode, so that it can lie dormant and restart when I re-open my laptop?

A: 

You can catch WM_POWERBROADCAST window message with PBT_APMQUERYSUSPEND event inside it. To catch this message inside Python program, you can create new invisible window and make separate thread repeatedly calling GetMessage().

In the worst case you can archieve all these by using ctypes only, but you can also use pywin32, sometimes referred to as win32py.

modchan