MsgWaitForMultipleObjectsEx 死锁 MFC 应用程序中的主 STA 线程
最好的办法是做STA线程同步?
我有一个基于对话框的 MFC 应用程序,有两个 STA 线程:主线程和第二线程。我在主 UI 线程中创建了 COM 对象。
我从第二个线程调用 COM 接口。在主线程中,为了确保第二个线程在主线程继续之前完成,我使用循环来等待该线程。此循环循环调用 MsgWaitForMultipleObjectsEx(timout=500)。
我立刻陷入了等待的僵局。然后我意识到 MFC 消息泵 AfxInternalPumpMessage() 只能帮助阻止调用,但对死锁情况没有任何帮助。
然后我将 Peek/Translate/DispatchMessage 代码添加到等待循环中,然后一切正常。
现在,似乎如果不手动编码 Peek/Translate/DispatchMessage 循环,您根本无法进行 STA 线程同步?任何等待电话都会让你陷入僵局。那么同步 STA 线程的最佳或标准方法是什么?
谢谢!
What is the best was to do STA thread synchronization?
I have a dialog based MFC application with two STA threads: the main and the second. I created the COM object in the main UI thread.
I call the COM interface from the second thread. In the main thread, to make sure the second thread finishes before the main thread proceeds, I use a loop to wait for the thread. This loop calls MsgWaitForMultipleObjectsEx(timout=500) looply.
I immediately got deadlocked on the wait. Then I realized that the MFC message pump, AfxInternalPumpMessage(), only helps with blocking calls but doesn't help at all with deadlock situations.
Then I added Peek/Translate/DispatchMessage code into the wait loop and then all worked.
Now, it seems that without hand coding the Peek/Translate/DispatchMessage loop, you can't do STA threads synchronization at all? Any wait call will deadlock you. Then what's the best or standard way to synchronize STA threads?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您正在寻找
CoWaitForMultipleHandles
?
Perhaps you are looking for
CoWaitForMultipleHandles
?不是任何等待,而是任何停止运行消息循环的等待确实会使您陷入僵局。解决方案是将等待循环和消息循环处理交错 - 稍等一下,然后处理所有待处理的消息,然后重复。
Not any wait, but any wait that stops running the message loop will indeed deadlock you. The solution is to interleave your wait loop and message loop processing - wait a little, then process all pending messages, repeat.