PRB: Message Broadcasting Process Causes DeadlockLast reviewed: October 21, 1997Article ID: Q175332 |
The information in this article applies to:
SYMPTOMSA deadlock results when a process waits on the handle of another process that broadcasts a message.
CAUSEThis occurs when the waiting process is waiting in the thread that processes the messages sent to the window (the main window thread).
RESOLUTIONTo prevent this deadlock situation the waiting process can wait in a separate thread, leaving the main window thread free to process window messages, or use the MsgWaitForMultipleObjects() API in conjunction with the dispatching messages.
MORE INFORMATIONTo receive notification when a process has terminated, a Win32 application may call WaitForSingleObject() and specify the process handle of the application to be waited on. Normally, this will cause the waiting process to block until the application being waited on terminates. Then WaitForSingleObject() will return WAIT_OBJECT_0 and the waiting process will continue to execute. Sometimes applications broadcast messages to some or all other windows on the system. In this event the above method will cause a deadlock situation if the waiting application is not free to process messages. If the waiting application is waiting in its thread that processes window messages, then no messages can be processed until the wait is complete. If the process waited on calls SendMessage() and broadcasts a message to the window of the waiting process, then a deadlock results. The SendMessage() will not return until the message is processed, and the waiting process cannot dispatch the message until its WaitForSingleObject() call returns. For a description of a common OLE manifestation of this problem, please refer to the following Knowledge Base article:
ARTICLE-ID: Q136885 TITLE : INFO: OLE Threads Must Dispatch MessagesTo prevent this deadlock, the waiting application should wait in a separate thread, thus freeing the main thread to process messages. When the process that is being waited on completes and WaitForSingleObject() returns, the waiting thread can notify the main thread by sending a user-defined message to the message queue of the main window. The main thread will interpret the user-defined message as completion of the waited-on process. For more information about user-defined messages, please refer to the following Knowledge Base article:
ARTICLE-ID: Q86835 TITLE : HOWTO: Define Private Messages for Application UseAnother way to avoid this problem is to have the waiting thread call MsgWaitForMultipleObjects() in a loop, which calls DispachMessage() when a message needs to be processed. For an example of this technique see the Knowledge Base article referenced above on the OLE problem (Q136885).
|
Additional query words: Setup
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |