1
votes

This is my 2nd question here on SO, hope I didn't screw up anything!

The question is theoretical because my implementation seems working perfectly, I just want to be sure it's ok.

I create mulitple threads that as part of their work PostMessage() to the main thread. The main thread then waits for all the threads to terminate. After all threads terminated the main thread calls Application.ProcessMessages. So the question is after this call is it sure that all the messages are received?

1
Depends on what the function returns, zero specifies failure. Apart from that, yes, ProcessMessages will remove messages from the queue until there is none.Sertac Akyuz
In addition to what @SertacAkyuz says, it depends on the specific message being sent. In some cases, Windows will combine multiple messages into a single message.Ken White
@KenWhite AFAIK, that is only true for system-generated messages, not for user-issued messages.Remy Lebeau
@RemyLebeau: I don't see anything that differentiates the source of the message, just the message itself. There aren't enough specifics in this post to state that all calls to PostMessage will succeed.Ken White

1 Answers

1
votes

If PostMessage() returns nonzero, it is guaranteed that the message has been put into the message queue of the thread that owns the window being posted to.

Application.ProcessMessages() is a blocking function. It does not exit until the message queue of the calling thread has been completely cleared of pending messages.

Now, whether a posted message actually reaches the message procedure of the window it is posted to is another matter. There are factors that can prevent that. Bad message queue filtering. The window being destroyed before the message is removed from the queue. Etc. However, given the example you describe, it is unlikely any of those would occur.

So yes, once all of the threads have fully terminated, and a subsequent call to Application.ProcessMessages() has exited, you are guaranteed to not receive any further messages from the threads.