Article ID: 136885 - View products that this article applies to.
This article was previously published under Q136885
Single-Threaded Apartment (STA) threads that do not dispatch messages can cause message broadcasters to hang. STA server threads that do not dispatch messages are not able to receive calls from clients. STA client threads that do not dispatch messages are not able to receive notifications from servers. Any STA thread that does not dispatch messages will leak small amounts of memory.
This article only applies to threads that are single-threaded apartment model. Threads in the MTA (Multi-Threading Apartment) using the free- threading model introduced in Windows NT version 4.0 and Distributed COM for Windows 95 are not required to dispatch messages. For additional information on free threading, please see the following article in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/150777/EN-US/ )Descriptions and Workings of OLE Threading Models
In Windows NT 3.51 and Windows 95, OLE creates a hidden, top-level window in each STA thread. (An STA thread is a thread that has called Co/OleInitialize.) If someone broadcasts a message (like WM_DDE_INITIATE) to all windows, the SendMessage hangs until all top-level windows have received the message. To prevent the broadcaster from hanging, OLE requires each STA thread to dispatch messages.
32-bit OLE calls between processes are made using RPC. In Apartment model threading and in single threading, OLE synchronizes the received RPC call with the recipient thread by posting the thread a window message. Consequently a server OLE thread doesn't receive calls from clients if it doesn't dispatch messages. Similarly a client OLE thread doesn't receive notifications from servers if it doesn't dispatch messages.
For example, the following thread blocks can encounter the problems discussed earlier:
You can fix this code by replacing WaitForSingleObject with a message loop function as in this example:
Any STA thread that does not block but makes OLE calls, needs to dispatch messages as follows.
Article ID: 136885 - Last Review: March 16, 2005 - Revision: 3.3