对于已被破坏的窗口的待处理消息会如何处理? [英] What happens to pending messages for a window that has been destroyed?

查看:97
本文介绍了对于已被破坏的窗口的待处理消息会如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在仍然有消息挂起的情况下销毁窗口时会发生什么?

What happens when a window is destroyed while there are still messages pending for it?

请考虑以下情形:

共有三个线程,A,B和C.线程C拥有一个窗口.

There are three threads, A, B, and C. Thread C owns a window.

线程A和B使用SendMessage将消息发布到窗口.来自A的消息首先到达. C处理来自A的消息时,会使用DestroyWindow破坏其窗口.

Threads A and B use SendMessage to post messages to the window. The message from A arrives first. While C is processing the message from A, it destroys its window using DestroyWindow.

来自线程B的消息会如何处理?线程B对SendMessage的调用是否返回?

What happens to the message from thread B? Does the call by thread B to SendMessage return?

这是如何在内部工作的?

How does this work internally?

推荐答案

原则上,您建议做的事并不安全.线程C无法保证线程B已经发送了消息.如果在线程B发送消息之前销毁了窗口,并且与此同时如果窗口句柄被重用,则线程B可能最终会将消息发送给错误的窗口,而该窗口可能在其他应用程序中.

In principle, what you're proposing to do isn't safe. There's no way for thread C to guarantee that thread B has already sent the message; if the window is destroyed before thread B sends the message, and if the window handle happens to get reused in the meantime, thread B might wind up sending the message to the wrong window, which might be in a different application.

最佳做法是确保在调用DestroyWindow之前,所有线程都已被告知特定的窗口句柄已变为无效.

The best practice would be to make sure that all threads have been informed that a particular window handle has become invalid before calling DestroyWindow.

但是,实际上,在错误的时间重新使用手柄的风险非常低.如果不希望提前通知其他线程,那么您就不太可能陷入麻烦.我相信kicsit断言消息不会最终在线程C的消息队列中等待是正确的,尽管据我所知文档并未明确承诺.

However, practically speaking, the risk of the handle being reused at just the wrong time is very low. If it is not plausible to inform the other threads ahead of time, you are unlikely to get into trouble as a result. I believe that kicsit is right in asserting that the message will not end up waiting in thread C's message queue, although the documentation does not explicitly promise this as far as I can tell.

这篇关于对于已被破坏的窗口的待处理消息会如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆