SendMessageCallback() - what is execution context of callback function

I have a technique that I use in WinAPI programs, to avoid stalling the message queue.

I start a separate thread in my program, which opens a hidden window with its own message queue. I typically call this thread the CommTask thread, since the first time I did this was to handle slow serial communications. When I want to perform a time-consuming task, I send a message to the CommTask thread using SendMessageCallback(), with a data packet telling it what to do.

When the CommTask thread completes handling of my message, my callback function gets executed.
What I don't know is, what thread is the callback function executing in?? Is it still in the CommTask thread, or has it somehow returned to my main program thread, or is there some other thread context that it executes in?
Why don't you check for yourself what thread you are in ? Use something like GetCurentThreadId or use thread local storage functions.

Do not use GetCurrentThread !
Aside from modoran's excellent suggestion that this would be trivial to verify for yourself (you could just set a breakpoint on your callback function and see which thread is running), the answer is obvious: the callback runs on the thread which called SendMessageCallback. That is the only answer that makes any sense. What would happen if you called the function with a window handle owned by another process?

Further, the MSDN documentation (http://msdn.microsoft.com/en-us/library/windows/desktop/ms644951(v=vs.85).aspx) answers the question for you:

If the target window belongs to a different thread from the caller, then the callback function is called only when the thread that called SendMessageCallback also calls GetMessage, PeekMessage, or WaitMessage.
Topic archived. No new replies allowed.