Home : COM / OLE / ActiveX notes : Apartments and threading
Before each thread can use COM, it must call CoInitialize[Ex]. The call specifies which type of "apartment" the thread wishes to join:
Single-threaded apartment: a new single-threaded "apartment" is created, with the calling thread as the only member. The first such apartment is called the main apartment of the process.
Multi-threaded apartment: the thread joins other threads in the multiple-threaded "apartment". Each process only has one multiple-threaded apartment.
Each COM object is registered (using the ThreadingModel value) as following one of the following threading models. When created, a COM object belongs to one "apartment". Only threads belonging to that apartment may legally call the object directly. Other threads must call the object via proxies.
none: the COM object may only belong to the main apartment of the process.
Apartment: different instances of the COM object may belong to different single-threaded apartments. Each COM object will only be called by a single thread, but different instances may be called by different threads. Any data in the COM object which is shared between instances must be protected from multiple access.
Free: the COM object may only belong to the multi-threaded apartment. Each COM object may be called from multiple threads. Any data in the COM object which is shared between instances, and data exclusive to each instance, must be protected from multiple access.
Both: the COM object follows both the "apartment" and the "free" model, so can belong to any type of apartment.