0

In C++, when I call CoInitialize() an STA is being created an associated to thread t that is calling "CoInitialize()". Now, if I understand correctly, when t creates a COM object, the object is associated with the STA that is associated with t, which means that only "t" can make calls to this object.

My question is what happens in C#? Who call CoInitialize()?

Some background: my question raised from a thread impersonation problem. A thread is impersonated to a user and then it calls COM object, does the thread that calls the COM object is really the thread that is executing the code inside (inside the COM objects there are no new threads).

Thanks a lot!

TCS
  • 5,790
  • 5
  • 54
  • 86
  • Dear downvoters, if you down vote, please say why, otherwise your downvote is useless and not constructive. – TCS Oct 02 '13 at 11:59

2 Answers2

2

COM objects are single threaded and cannot have multiple threads. STA is responsible for syncronization of multiple consumer of COM component.

When multiple clients call an object, the calls are queued in the message queue and the object will receive a call each time its apartment retrieves and dispatches messages. Because the calls are synchronized by COM and the calls are always delivered by the thread that belongs to the object's apartment, the object's interface implementations need not provide synchronization, MSDN.

STA and MTA

For interoperability, the common language runtime creates and initializes an apartment when calling a COM object. A managed thread can create and enter a single-threaded apartment (STA) that contains only one thread, or a multi-threaded apartment (MTA) that contains one or more threads. When a COM apartment and a thread-generated apartment are compatible, COM allows the calling thread to make calls directly to the COM object. If the apartments are incompatible, COM creates a compatible apartment and marshals all calls through a proxy in the new apartment, reference

CLR calls CoInitializeEx

The runtime calls CoInitializeEx to initialize the COM apartment as either an MTA or an STA apartment. In the .NET Framework version 2.0, managed threads are initialized as MTA if their apartment state has not been set prior to starting the thread. Use the SetApartmentState or TrySetApartmentState method to set the apartment state before starting the thread, reference;

Adil
  • 146,340
  • 25
  • 209
  • 204
  • so every COM object (in STA) has a thread dedicated only to execute its functions, like active object?! Or is it one of my thread that called "CoInitialize()" and created the object? – TCS Oct 02 '13 at 11:58
  • No, all COM objects in the STA share a single thread, namely, the thread that called `CoInitialize()`. – Eric Brown Oct 02 '13 at 17:00
0

To further answer your question, in C#, the CLR calls CoInitialize on your behalf. You can set the threading model using either the [STAThread] attribute on your main method, or by setting the ApartmentState property of the thread.

Eric Brown
  • 13,774
  • 7
  • 30
  • 71
  • Please take a look at [this related question](http://stackoverflow.com/q/29385515/938668). I am not able to figure out if the call to `CoInitializeEx` should go on the main thread before calling `Thread.Start` or from within the delegate of the thread. – Raheel Khan Apr 01 '15 at 09:26