views:

138

answers:

3

Is there any API to send message to a thread? Basically I have only threadId available and I want to send a custom message to that thread.

A: 

If you use .NET, maybe this is udeful: http://www.codeproject.com/KB/threads/threadlib.aspx

Konamiman
I forget to add C++ tag.
Alien01
+4  A: 

PostThreadMessage. Not very reliable though.

See The Old New Thing blog here and here for details on why. Basically modal message loops make a mess of the whole idea. Since a message posted to a thread has no window handle, calling DispatchMessage will throw the message away. Any modal loop you run - directly or indirectly - will call DispatchMessage, so a good proportion of the time this strategy will fail and your message will disappear into the ether.

Bob Moore
Most important sentence from those two articles: "If you need to communicate reliably with a thread that also displays UI, then create a hidden window and send or post messages to that window."
Thomas
Of course, if _all you have is a ThreadId_, that suggestion won't work either.
MSalters
A: 

you can use QueueUserAPC if the target thread is alertable

Bevan Collins