Reputation:
I have read two contradictory concepts about calling SendMessage()
from another thread:
The first is that a message will be posted to the message queue.
The second is that SendMessage()
will be called directly. 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.
So which one is correct?
Upvotes: 2
Views: 2123
Reputation: 612964
The documentation for SendMessage
is, in my view, reasonably clear:
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. 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. There are other functions, e.g. PeekMessage
, SendMessage
etc., that will dispatch messages.
Upvotes: 5