PRB: PostThreadMessage Messages Lost When Posted to UI Thread

ID: Q183116

The information in this article applies to:

SYMPTOMS

Messages sent to a UI thread through PostThreadMessage are lost if the messages are posted while the user is manipulating a window owned by the thread. Messages might be lost if they are sent while the user moves or resizes the window or if the window is a modal dialog box.

CAUSE

When an UI thread is involved in modal behavior, the thread pumps messages in a message loop internal to the modal system rather than in the thread's main message loop. Messages that are posted to a window can still be dispatched to the window procedure of the target window, because the messages are associated with a window. However, thread messages need to be handled directly by the message loop, because they cannot be automatically dispatched elsewhere. Since the secondary message loop does not know about the thread message, it will be dropped.

RESOLUTION

Rather than using PostThreadMessage to post messages to a UI thread, use PostMessage to post messages to a window owned by that thread. Since messages directed to a window can be dispatched by a secondary message loop, the messages is still handled properly, even when the thread is not running in its primary message loop.

To use this method, the thread must create a window that lives throughout the period that it must receive messages, and the application sending the messages must have a handle to that window available.

STATUS

This behavior is by design.

Additional query words:

Keywords          : kbNTOS kbGrpUser kbWinOS kbWndw kbWndwMsg 
Issue type        : kbprb

Last Reviewed: January 2, 1999