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?
Asked
Active
Viewed 2,333 times
2
-
Windows Vista SP1 64-bit Python 2.6 compiled in 32-bit mode – user235024 Dec 31 '09 at 04:35
1 Answers
0
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
.

toriningen
- 7,196
- 3
- 46
- 68
-
Does this only work with GUI as this python programming is running as a service. Will the same thing work by calling GetMessage() in a thread running within a service? – user204088 Oct 10 '14 at 14:33
-
@user204088, it depends. You can either enable interactive user for your service, so you will be able to create invisible window directly, but this require console user to be logged on; or use `RegisterServiceCtrlHandlerEx` and listen for `SERVICE_CONTROL_POWEREVENT` in your `HandlerEx` – toriningen Oct 10 '14 at 19:26