中途结束程序
pythoncom.PumpMessages()
据我了解,这一行基本上告诉程序永远等待。就我的目的而言,它似乎有效。然而,我希望能够在适当的刺激下结束该计划。如何结束上述行,或停止程序进一步运行。
pythoncom.PumpMessages()
From what I understand this line basically tells the program to wait forever. For my purposes it seems to be working. However, I'd like to be able to end the program given the right stimulus. How would one go about ending the above line, or stopping the program from running any further.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据这些 docs,
pythoncom.PumpMessages()
:因此,停止收集消息的一种方法是使用 ctypes 库来调用 PostQuitMessage:
According to these docs,
pythoncom.PumpMessages()
:So one way to stop collecting messages is by posting a WM_QUIT message to the message queue by using the ctypes library to call PostQuitMessage:
下面是使用计时器线程退出应用程序的示例:
PostQuitMessage()
只能在主线程中工作,但主线程又被阻塞了,所以它不是很有效。本身很有用。仅当您将自己的自定义消息处理挂接到消息循环中时才能使用它。Here's an example of quitting the app using a timer thread:
PostQuitMessage()
will work only from the main thread, but then again the main thread is blocked, so it's not very useful by itself. You can only use it if you hook your own custom message handling into the message loop.我想扩展 Gregg 和 波阿斯·亚尼夫。您通常会在单独的线程中运行阻塞代码,因此您需要向该线程发送 WM_QUIT。您应该使用 PostQuitMessage 正如 Gregg 所指出的,但这只适用于当前线程。您不应该使用 PostThreadMessage< /a> 发送 WM_QUIT (不记得我在文档中看到它的位置)。您可以在讨论“为什么有一个特殊的 PostQuitMessage 中阅读更多相关信息功能?”。我认为最好先发送WM_CLOSE到线程。
我将它用于 RegisterHotKey 但原理是相同的。这个类可以这样调用:
当你想结束等待消息调用时:
I would like to extend both answers by Gregg and Boaz Yaniv. You would normally run blocking code in separate thread therefore you need to send WM_QUIT to the thread. You should use PostQuitMessage as noted by Gregg but that works only in current thread. You shouldn't use PostThreadMessage to send WM_QUIT (can't remember where I see it in docs). You can read more about it in discussion "Why is there a special PostQuitMessage function?". I think it's better to send WM_CLOSE to the thread first.
I am using it for RegisterHotKey but principle is same. This class could be called as:
When you want to end waiting for messages call: