简体   繁体   English

是否从另一个线程调用SendMessage()将消息发布到消息队列?

[英]Does a SendMessage() call from another thread posts a message to the message queue?

I have read two contradictory concepts about calling SendMessage() from another thread: 我已经阅读了有关从另一个线程调用SendMessage()两个矛盾的概念:

  • The first is that a message will be posted to the message queue. 首先是将一条消息发布到消息队列中。

  • The second is that SendMessage() will be called directly. 第二个是SendMessage()将被直接调用。 However, the calling thread will block, and the context is switched to the UI thread, and then the window procedure is called, and when it returns, the calling thread is unblocked and the context is switched back to it. 但是,调用线程将阻塞,并且上下文切换到UI线程,然后调用窗口过程,并且在返回窗口过程时,将取消阻塞调用线程,并将上下文切换回它。

So which one is correct? 那么哪一个是正确的呢?

The documentation for SendMessage is, in my view, reasonably clear: 在我看来, SendMessage的文档相当清晰:

If the specified window was created by the calling thread, the window procedure is called immediately as a subroutine. 如果指定的窗口是由调用线程创建的,则该窗口过程将作为子例程立即被调用。 If the specified window was created by a different thread, the system switches to that thread and calls the appropriate window procedure. 如果指定的窗口是由其他线程创建的,则系统切换到该线程并调用适当的窗口过程。 Messages sent between threads are processed only when the receiving thread executes message retrieval code. 仅当接收线程执行消息检索代码时,才处理线程之间发送的消息。 The sending thread is blocked until the receiving thread processes the message. 发送线程被阻塞,直到接收线程处理该消息为止。

Messages sent with SendMessage are never placed on the message queue. SendMessage发送的消息永远不会放在消息队列中。 In other words, your first bullet point is completely false. 换句话说,您的第一个要点是完全错误的。

In the case where a message is sent cross-thread, it is dispatched in the thread that owns the window, typically by calls to GetMessage in the recipient thread's message loop. 如果消息是跨线程发送的,则通常在接收者线程的消息循环中通过调用GetMessage来将消息分派到拥有窗口的线程中。 There are other functions, eg PeekMessage , SendMessage etc., that will dispatch messages. 还有其他功能,例如PeekMessageSendMessage等,将调度消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM