Posting Messages to a Thread s Message Queue

[Previous] [Next]

Once a thread has a THREADINFO structure associated with it, the thread also has its very own set of message queues. If a single process creates three threads and all these threads call CreateWindow, there will be three message queue sets. Messages are placed in a thread's posted-message queue by calling the PostMessage function:

 BOOL PostMessage( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 

When a thread calls this function, the system determines which thread created the window identified by the hwnd parameter. The system then allocates a block of memory, stores the message parameters in this block of memory, and appends the block of memory to the appropriate thread's posted-message queue. In addition, the function sets the QS_POSTMESSAGE wake bit (I'll discuss this shortly). PostMessage returns immediately after posting the message—the calling thread has no idea whether the posted message was processed by the specified window's window procedure. In fact, it is possible that the specified window will never receive the posted message. This could happen if the thread that created the specified window were somehow to terminate before processing all of the messages in its queue.

A message can also be placed in a thread's posted-message queue by calling PostThreadMessage:

 BOOL PostThreadMessage( DWORD dwThreadId, UINT uMsg, WPARAM wParam, LPARAM lParam); 

NOTE

You can determine which thread created a window by calling GetWindowThreadProcessId:

 DWORD GetWindowThreadProcessId( HWND hwnd, PDWORD pdwProcessId); 

This function returns the system-wide unique ID of the thread that created the window identified by the hwnd parameter. You can also get the system-wide unique ID of the process that owns this thread by passing the address of a DWORD for the pdwProcessId parameter. Usually, we do not need the process ID and simply pass NULL for this parameter.

The desired thread is identified by the first parameter, dwThreadId. When this message is placed in the queue, the hwnd member in the MSG structure will be set to NULL. This function is usually called when an application performs some special processing in its main message loop.

The main message loop for the thread is written so that, after GetMessage or PeekMessage retrieves a message, the code checks for an hwnd of NULL and can examine the msg member of the MSG structure to perform the special processing. If the thread determines that this message is not destined for a window, DispatchMessage is not called, and the message loop iterates to retrieve the next message.

Like the PostMessage function, PostThreadMessage returns immediately after posting the message to the thread's queue. The calling thread has no idea when or if the message gets processed.

The last function that posts a message to a thread's queue is PostQuitMessage:

 VOID PostQuitMessage(int nExitCode); 

You call this function in order to terminate a thread's message loop. Calling PostQuitMessage is similar to calling

 PostThreadMessage(GetCurrentThreadId(), WM_QUIT, nExitCode, 0); 

However, PostQuitMessage doesn't really post a message to any of the THREADINFO structure's queues. Internally PostQuitMessage just turns on the QS_QUIT wake flag (which I'll discuss later) and sets the nExitCode member of the THREADINFO structure. Since these actions can never fail, PostQuitMessage is prototyped as returning VOID.



Programming Applications for Microsoft Windows
Programming Applications for Microsoft Windows (Microsoft Programming Series)
ISBN: 1572319968
EAN: 2147483647
Year: 1999
Pages: 193

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net