Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I SendMessage() to a window created on another thread?

I have a situation where I want to SendMessage to a window that was created on another thread than the one that is calling SendMessage.

The default behaviour seems to be block forever and not work.

So I changed the call to PostMessage, which didn't block the sending thread, but the message never appears to arrive at the intended window.

So how do I SendMessage to a window created on a separate thread, or is this impossible?

like image 205
bobobobo Avatar asked Aug 15 '11 00:08

bobobobo


1 Answers

What is the thread that owns the target window doing? It needs to be pumping messages in order to be able to receive messages that are either sent or posted to it. Typically this is done by having a GetMessage/TranslateMessage/DispatchMessage loop. If the thread is doing something else - such as blocking waiting for an event or mutex or IO to complete, or is busy in some other loop carrying out a calculation, it won't receive messages: SendMessage to the thread will block, and PostMessage will post but not get delivered.

If that target thread needs to both manage events or similar, and it also owns a window, you may need to use MsgWaitForMultipleObjects in that thread's message loop.

like image 117
BrendanMcK Avatar answered Sep 30 '22 07:09

BrendanMcK