Hi all
i have been reading a lot on COM Threading models and have some real time experience developing COM code, but one aspects of STA and need for Message loop have always puzzled me, hope experts here can help me understand this better.
My understanding of COM STA model:
Client code calls CoInitialize() and it enters an apartment(STA) , the COM Dll(SERVER) gets loaded and based on threading model registry entry it joins the same apartment, client application making calls on the Interface pointer , is translated into a windows message and posted onto the server which already has a hidden window created and has a window procedure associated with in and hence calls are synchronized.
MY Questions:
Q1) What does this following article trying to explain, why there is a need for writing our own custom message pump code.
Failure to Pump
Let’s look at those last two bullet points in more detail. When your STA thread is doing nothing else, it needs to be checking to see if any other threads want to marshal some calls into it. This is done with a Windows message pump. If the STA thread fails to pump, these incoming calls will be blocked. If the incoming calls are GUI SendMessages or PostMessages (which I think of as synchronous or asynchronous calls respectively), then failure to pump will produce an unresponsive UI. If the incoming calls are COM calls, then failure to pump will result in calls timing out or deadlocking.
If processing one incoming call is going to take a while, it may be necessary to break up that processing with intermittent visits to the message pump. Of course, if you pump you are allowing reentrancy to occur at those points. So the developer loses all his wonderful guarantees of single threading.
Unfortunately, there’s a whole lot of STA code out there which doesn’t pump adequately. For the most part, we see this in non-GUI applications. If you have a GUI application that isn’t pumping enough, it’s obvious right there on the screen. Those bugs tend to get fixed.
For non-GUI applications, a failure to pump may not be noticed in unmanaged code. When that code is moved to managed (perhaps by re-compiling some VB6 code as VB.NET), we start seeing bugs. Let’s look at a couple of real-world cases that we encountered during V1 of the CLR and how the lingering effects of these cases are still causing major headaches for managed developers and for Microsoft Support. I’ll describe a server case first, and then a client case.
Q2) The Book by DonBox states that "THe STA Thread must service the queue via some variation on the following code"
MSG msg;
while(GetMessage(&msg,0,0,0))
DispatchMessage(&msg);
so what i understood is COM create a hidden window and Window procedure when CoInitialize is called, does it not implement code for the message loop? but i have written many STA COM Dlls and never wrote any code for creating a message loop and all the COM calls works fine.
can anyone clarify my questions.
regards
Rekha