33.2.3 Posting a Message

You can post a message to a message queue by using the PostMessage function. PostMessage places a message at the end of a thread's message queue and returns immediately without waiting for the thread to process the message. The parameters of the PostMessage function include a window handle, a message identifier, and two message parameters. Windows copies these parameters to an MSG structure, fills the time and pt members of the structure, and places the structure to the message queue.

Windows uses the window handle passed with the PostMessage function to determine which thread message queue should receive the message. If the handle is 0xFFFFFFFF, Windows posts the message to the thread message queues of all top-level windows.

You can use the PostThreadMessage function to post a message to a specific thread message queue. PostThreadMessage is similar to PostMessage except that the first parameter is a thread identifier rather than a window handle. You can retrieve the thread identifier by calling the GetCurrentThreadId function.

You can use the PostQuitMessage function to exit a message loop. PostQuitMessage posts the WM_QUIT message to the currently executing thread. The thread's message loop should terminate and return control to Windows when it encounters the WM_QUIT message. An application typically calls PostQuitMessage in response to the WM_DESTROY message, as shown in the following example:

case WM_DESTROY:

.

. /* Perform clean-up tasks. */

.

PostQuitMessage(0);

break;