Groups | Blog | Home
all groups > dotnet interop > august 2007 >

dotnet interop : COM Object Class Library Threading model


Davinci_Jeremie
8/23/2007 8:04:04 AM
Hi

I have written an object in C# and exposed it as a COM object. This object
is used in a native application and that application creates one or more
threads that creates and uses its own copy of my C# COM object.

My C# COM object has reduced my performance of my native application because
all objects are create in a single threaded apartment. A call to
Thread.GetApartmentState() inside my C# COM object returns ApartmentState.STA.

Since the my C# COM object is in a class library I do not have a "Main"
method and can not apply the MTAThreadAttribute.

So my question is how do I tell my COM Class Factory to build objects that
are in a Multi threaded apartment in .NET C#?

If it's just not possible I would also like to know that as well.

Cheers,

Davinci
Davinci_Jeremie
8/23/2007 9:56:02 AM
Christian thanks for you response and no disrespect but when you said...

[quoted text, click to view]

This is true.

Then you said...

[quoted text, click to view]

This is not true as each copy operates in the same single threaded
apartment. Meaning a method call to instance 1 at the same time to instance
2 is serialized and only when the first call is completed the second call
will be executed even though the objects are different instances.

Then you went on to state...

[quoted text, click to view]

I am not doing that each instance of my COM object exists on different
threads that do not interact with each other.

You also said...

[quoted text, click to view]

This is true and my COM object is thread safe and does not (at least as far
as I know) access shared data.

Finally, are you saying there is no way to change it to MTA? If that's the
case that's fine I will need to work around it.

Thanks

Davinci_Jeremie
8/23/2007 12:28:05 PM
Thanks I think that may be my problem I will look into it.

Cheers

Davinci

[quoted text, click to view]
Christian Fröschlin
8/23/2007 5:29:59 PM
First of all, if each thread creates and uses it's own copy of the
COM object then STA is just fine, no penalty will occur unless you
access an object instance from a thread other than the one on
which the object was created.

Also, STA/MTA is a COM thing and can be specified in the native
app, but don't ask me how. Of course, your object needs to be
Mattias Sjögren
8/23/2007 9:16:19 PM

[quoted text, click to view]


How does your client initialize COM on those threads? Managed classes
registered for COM interop are set to ThreadingModel = Both, so they
should happily enter the MTA.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Christian Fröschlin
8/24/2007 12:00:00 AM
[quoted text, click to view]

A process can have multiple STA's, so I was assuming you have
one STA per thread. Actually, I'm used to this being the default
behavior at least for STA threads created in VB.NET or C#. I'm
actually not very familiar with the internals of COM, I just
know that using multiple COM objects in multi-threaded C#
applications with STA threads works fine without blocking
Davinci_Jeremie
8/28/2007 11:24:51 AM
That was exactly my problem. Here is how to resolve it...

An exported managed server (COM Object) with a type library registered by
the Assembly Registration tool (Regasm.exe) has a ThreadingModel registry
entry set to Both. This value indicates that the server can be activated in a
single-threaded apartment (STA) or a multithreaded apartment (MTA). The
server object is created in the same apartment as its caller.

Because the client and server are in the same apartment, the interop
marshaling service automatically handles all data marshaling.

Thus, you can use the CoInitializeEx(NULL, COINIT_MULTITHREADED) from your
native thread to ensure that the objects are created in MTA. (Please make
sure that CoInitializeEx() is called at the earliest point in the execution
of the thread, with a matching CoUninitialize at the end.)



[quoted text, click to view]
AddThis Social Bookmark Button