PRB: Message Broadcasting Process Causes Deadlock

Last reviewed: October 21, 1997
Article ID: Q175332
The information in this article applies to:
  • Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT versions 3.51, 4.0 - Microsoft Windows 95

SYMPTOMS

A deadlock results when a process waits on the handle of another process that broadcasts a message.

CAUSE

This occurs when the waiting process is waiting in the thread that processes the messages sent to the window (the main window thread).

RESOLUTION

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.

MORE INFORMATION

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:

   ARTICLE-ID: Q136885
   TITLE     : 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:

   ARTICLE-ID: Q86835
   TITLE     : 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).


Additional query words: Setup
Version : WINNT:3.51,4.0;
Platform : Win95 winnt
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: October 21, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.