You definately cannot invoke the COM object from an MTA thread.
Ideally you want to just call methods from the STA thread that created
the object. IIRC, you should be able to call from one STA thread to
another if you have a typelib registered which allows the cirrect
marshalling to happen.
ziph...@gmail.com wrote:
> You definately cannot invoke the COM object from an MTA thread.
Interesting that you should say that: I checked the Main method of the
application im working on (i didnt write that myself). It is tagged
with [STAThread], so i suppose all the threads its spawns are STA as
well. I tried changing it to MTAThread (which im not too fond of since
there is a comment saying another component is dependent on STAThread)
and all my problems went away! So it seems that i can indeed call it
from an MTA thread. As far as i understand its the apartment mode of
the thread that creates the COM object that decides the apartment mode,
so when it is created with an MTA thread and only accessed by MTA
threads it seems to work.
When i previously tried spawning a MTA thread to create it, the other
STA threads couldnt access the COM object.
> Ideally you want to just call methods from the STA thread that created
> the object.
That is sadly not possible(or at least not worth the bother), the COM
object in question is a DirectShow filter which will be accessed by the
graph thread and additionally my own thread. I get the abovementioned
error even if the COM object calls back to a managed method and tries
to call its own methods (ie it notifies my of a new subtitle, and i
call a method to get it, within the callback thread).
> IIRC, you should be able to call from one STA thread to
> another if you have a typelib registered which allows the cirrect
> marshalling to happen.
Do you know where more information can be found on this?
TDC - 14 Jan 2007 19:22 GMT
COM objects have to be specifically coded to support MTA and most do
not, so I assumed your problem was calling an STA-oly object from an
MTA thread. When you register the object dll, it will show the
apratment type supported in the registry, but it seems like you've
already determined it supports MTA.
As an aside, there is nothing in the .NET SDK that states that threads
always spawn threads of the same type. You should explicitly use the
thread type you need.
Finally, the best resource is a book entitled .NET and COM: The
Complete Interoperability Guide. It's easily over 800 pages, if that
gives an idea of it's depth of coverage.
Tom
> > You definately cannot invoke the COM object from an MTA thread.
>
[quoted text clipped - 27 lines]
>
> Do you know where more information can be found on this?
ziphnor@gmail.com - 21 Jan 2007 15:11 GMT
> When you register the object dll, it will show the
> apratment type supported in the registry, but it seems like you've
> already determined it supports MTA.
Where exactly? I looked under the CLSID in the registry, but it doesnt
seem to store such information. As far as i understand DirectShow
filters are supposed to be 'free' threaded objects btw.
> As an aside, there is nothing in the .NET SDK that states that threads
> always spawn threads of the same type. You should explicitly use the
> thread type you need.
Sure, but even if i create an MTA thread and create the object there,
the other threads cannot access it, so there is some significance to
the main threads apartment model. I suppose it might be related to the
'Main STA' concept described here:
http://www.idevresource.com/com/library/articles/aparts.asp#multiap
> Finally, the best resource is a book entitled .NET and COM: The
> Complete Interoperability Guide. It's easily over 800 pages, if that
> gives an idea of it's depth of coverage.
Okay thanks :)
TDC - 22 Jan 2007 13:47 GMT
> > When you register the object dll, it will show the
> > apratment type supported in the registry, but it seems like you've
[quoted text clipped - 3 lines]
> seem to store such information. As far as i understand DirectShow
> filters are supposed to be 'free' threaded objects btw.
Drill into InprocServer32\ThreadingModel.