[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.interop

COM Object Class Library Threading model

Davinci_Jeremie

8/23/2007 3:04:00 PM

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

6 Answers

Christian Fröschlin

8/23/2007 3:30:00 PM

0

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
thread-safe to meaningfully run in an MTA.

Davinci_Jeremie

8/23/2007 4:56:00 PM

0

Christian thanks for you response and no disrespect but when you said...

> First of all, if each thread creates and uses it's own copy of the
> COM object.

This is true.

Then you said...

> then STA is just fine,

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...

> unless you
> access an object instance from a thread other than the one on
> which the object was created.

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...

> Of course, your object needs to be
> thread-safe to meaningfully run in an MTA.

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

(Mattias Sjögren)

8/23/2007 7:16:00 PM

0


>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.


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.n... | http://www.dotneti...
Please reply only to the newsgroup.

Davinci_Jeremie

8/23/2007 7:28:00 PM

0

Thanks I think that may be my problem I will look into it.

Cheers

Davinci

"Mattias Sjögren" wrote:

>
> >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.
>
>
> 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.n... | http://www.dotneti...
> Please reply only to the newsgroup.
>

Christian Fröschlin

8/24/2007 7:22:00 AM

0

> 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.

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
for me as long as the objects are created and used locally.

Davinci_Jeremie

8/28/2007 6:25:00 PM

0

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.)



"Mattias Sjögren" wrote:

>
> >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.
>
>
> 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.n... | http://www.dotneti...
> Please reply only to the newsgroup.
>