Sandeep
Wed Jul 27 08:01:05 CDT 2005
Hi Alex,
Thanks for your reply. Yes, you are right, the STA has been reentered due to
a call on B, as A has made a call to another STA which then has made an
out-of-proc call. So A cannot proceed until B finishes with its processing
(or makes an out-of-proc call) so that the thread in the STA is available
again.
> > I thought if A and B always belong to different STAs of
> > their own, this problem will not occur.
>
> I don't quite get it. You said first that A and B exist in
> the same STA. Also, if A and B exist in different
> appartments, then why it should prevent calls to B object
> while A is waiting?
>
Right, I only meant that if A and B live in different apartments, calls made
on either of them will not affect the other. The problem is because A and B
are in the same apartment and the apartment is reentered for a call on B
while call on A is waiting for an out-of-proc call to return. So making sure
(somehow) that A and B always exist in different apartments will get rid of
the issue of calls on B affecting A.
> To ensure that you need to create each object in separate
> thread. Then each object will have its own STA.
Yes, that's certainly a solution. Actually this has triggered another
question in my mind: how does COM decide on which apartment does a newly
created STA object go into. Is it always the case, that the object belongs to
the STA of the thread that created it?
Also, another important question in the same connection: if a client
residing in an MTA creates STA objects, how is it decided which apartments
these objects go into? Do they join any of the existing STAs or is a new STA
is created for any of them? Is there a concrete way of telling this?
Thanks again,
Regards,
Sandeep
"Alex Blekhman" wrote:
> Sandeep wrote:
> > I have the following problem:
> > I am getting two independent COM components (say A and B)
> > assigned a single STA. One of the components (say A)
> > calls a method on a third component C (in a different
> > STA) that may in turn possibly go out-of-proc from C. Now
> > as soon as this call goes out-of-proc, a call on
> > component B, which was waiting outside the apartment all
> > this time (as calls to STA are serialized by COM),
> > suddenly enters the STA and starts processing. Now, when
> > the out-of-proc call returns to C and then eventually
> > back to A, it has nowhere to return, as the thread is
> > already in use by B!
>
> What do you mean by "nowhere to return"? The call made from
> A doesn't disappear into void.
>
> What actually happens is that STA appartment is reentered
> while A is waiting in a call. See here last paragraph for
> explanation:
>
> "Single-Threaded Apartments"
>
http://msdn.microsoft.com/library/en-us/com/html/2f345ae2-8314-4067-a6d6-5a0275941ed4.asp
>
> It's perfectly legal from COM point of view to reenter the
> same STA. Think about it as if it was message procedure
> (which actually is) that shows modal dialog box. While
> dialog box is open procedure doesn't return. However, it
> continues to handle other messages in the same thread. So,
> while A component waits on out-of-proc call messages are
> dispatched and components (including A) can receive
> additional calls.
>
> > I thought if A and B always belong to different STAs of
> > their own, this problem will not occur.
>
> I don't quite get it. You said first that A and B exist in
> the same STA. Also, if A and B exist in different
> appartments, then why it should prevent calls to B object
> while A is waiting?
>
> > In that regard,
> > is there any way which can ensure that a COM component
> > with threadingmodel=Apartment (STA), after it is
> > CoCreated, will always lie in a different new STA, and
> > not join any other already existing STA?
>
> To ensure that you need to create each object in separate
> thread. Then each object will have its own STA.
>
> > Also, do you see any other possible solutions to this
> > problem?
>
> Probably you should slightly redesign A and B taking the
> possibility of reentrant call into account.
>
>
>