问题描述
我有一个与pythonw后台运行一个Python脚本。如果我关闭我的笔记本电脑,它进入休眠模式。当我打开我的笔记本电脑,我的程序没有什么功能,并几秒钟后冻结。有什么办法,如果我的电脑进入休眠模式我的脚本可以告诉,以便它可以处于休眠状态,然后重新启动,当我重新打开我的笔记本电脑?
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?
推荐答案
您可以赶上 WM_POWERBROADCAST
窗口消息, PBT_APMQUERYSUSPEND
里面的事件。为了赶上Python程序里面的这条消息,您可以创建新的不可见窗口,并单独的线程一边喊的GetMessage()
。
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()
.
在最坏的情况下,你可以通过使用archieve所有这些 ctypes的
而已,但你也可以使用的
,有时也被称为 win32py
。
In the worst case you can archieve all these by using ctypes
only, but you can also use pywin32
, sometimes referred to as win32py
.
这篇关于Python的计算机的睡眠/休眠后冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!