Microsoft KB Archive/175332
PRB: Message Broadcasting Process Causes Deadlock
The information in this article applies to:
- Microsoft Win32 Application Programming Interface (API), included with:
- the operating system: Microsoft Windows NT, versions 3.51, 4.0
- Microsoft Windows 95
To 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:
Q136885 INFO: OLE Threads Must Dispatch Messages
To 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:
Q86835 HOWTO: Define Private Messages for Application Use
Another 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).
A deadlock results when a process waits on the handle of another process that broadcasts a message.
This occurs when the waiting process is waiting in the thread that processes the messages sent to the window (the main window thread).
To 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.
Additional query words: Setup
Issue type : kbinfo
Technology : kbAudDeveloper kbWin32sSearch kbWin32API
Last Reviewed: December 16, 2000