COM Interop

Whenever you are doing COM Interop you must use a STA thread.
If you want to access a COM object from a thread, that thread must belong to an apartment.
COM provides threading guarantees for classes
COM objects can be used in threads.

1 Process - 1 STA

Also called Apartment Model
Is a set of COM objects associated with a single thread inside a process
The thread must retrieve and dispatch window messages

1 Process - Many STAs threads

A process can have more than one STA threads
Interface pointers are marshaled between the threads.

1 Process - 1 MTA

Added in .NET 4.0
Is a set of COM objects associated with multiple threads inside a process.
Interface pointers are NOT marshaled between the threads.

1 Process - 1 MTA, many STAs

Also called Mixed Model

STAThreadAttribute Class


Indicates that the COM threading model for an application is single-threaded apartment (STA).
Apply this attribute to the entry point method (the Main() method in C# and Visual Basic).
It has no effect on other methods.

COM threading models only apply to applications that use COM interop.
The COM threading model can be set to single-threaded apartment or multithreaded apartment.
The application thread is only initialized for COM interop if the thread actually makes a call to a COM component.
If COM interop is not used, then the thread is not initialized, and the STAThreadAttribute attribute, if it is present, has no effect.

Starting with the .NET Framework version 2.0, the default threading model for COM interop depends on the language in which you are developing your application:
Visual Basic - Single-threaded apartment
C# - Multithreaded apartment
C++ - Multithreaded apartment
To change these defaults, you use the STAThreadAttribute attribute to set the threading model for the application, or call the Thread.SetApartmentState or Thread.TrySetApartmentState method before starting the thread to set the threading model for a particular thread.

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext