Title: Could you explain STA and MTA?
Tags: .net;multithreading;com;apartments
Question: Can you explain STA and MTA in your own words?
Also, what are apartment threads and do they pertain only to COM? If so, why?
Comment: STAs allow COM components developed with tools (like VB6) that do not support thread synchronization to be used in multithreaded applications.

Comment: See Also: Understanding COM Apartments [Part 1](http://www.codeguru.com/cpp/com-tech/activex/apts/article.php/c5529/Understanding-COM-Apartments-Part-I.htm), [Part 2](http://www.codeguru.com/cpp/com-tech/activex/apts/article.php/c5533/Understanding-COM-Apartments-Part-II.htm)

Here is the accepted answer: The COM threading model is called an "apartment" model, where the execution context of initialized COM objects is associated with either a single thread (Single Thread Apartment) or many threads (Multi Thread Apartment). In this model, a COM object, once initialized in an apartment, is part of that apartment for the duration of its runtime.
The STA model is used for COM objects that are not thread safe. That means they do not handle their own synchronization. A common use of this is a UI component. So if another thread needs to interact with the object (such as pushing a button in a form) then the message is marshalled onto the STA thread. The windows forms message pumping system is an example of this.
If the COM object can handle its own synchronization then the MTA model can be used where multiple threads are allowed to interact with the object without marshalled calls.
Comment for this answer: A good read for further details: [INFO: Descriptions and Workings of OLE Threading Models](http://support.microsoft.com/kb/150777).

Here is another answer: Each EXE which hosts COM or OLE controls defines it's apartment state.  The apartment state is by default STA (and for most programs should be STA).
STA - All OLE controls by necessity must live in a STA.  STA means that your COM-object must be always manipulated on the UI thread and cannot be passed to other threads (much like any UI element in MFC).  However, your program can still have many threads.
MTA - You can manipulate the COM object on any thread in your program.
Comment for this answer: "STA means that your COM-object must be always manipulated on the UI thread"
I don't think this is exactly right...  it doesn't have to be on the "UI" thread, only an STA thread that has a message pump on it because the calls a synchronized using messages.  The UI thread will usually meet these requirements, but it is not the only possibility.

Here is another answer: STA (Single Threaded Apartment) is basically the concept that only one thread will interact with your code at a time.  Calls into your apartment are marshaled via windows messages (using a non-visible) window.  This allows calls to be queued and wait for operations to complete.
MTA (Multi Threaded Apartment) is where many threads can all operate at the same time and the onus is on you as the developer to handle the thread security.
There is a lot more to learn about threading models in COM, but if you are having trouble understanding what they are then I would say that understanding what the STA is and how it works would be the best starting place because most COM objects are STA’s.
Apartment Threads, if a thread lives in the same apartment as the object it is using then it is an apartment thread.  I think this is only a COM concept because it is only a way of talking about the objects and threads they interact with…

Here is another answer: Code that calls COM object dlls (for example, to read proprietary data files), may work fine in a user interface but hang mysteriously from a service.  The reason is that as of .Net 2.0 user interfaces assume STA (thread-safe) while services assume MTA ((before that, services assumed STA).  Having to create an STA thread for every COM call in a service can add significant overhead.

Here is another answer: This article explains STA &amp; MTA very clearly.
Understanding COM Apartments, Part I
Understanding COM Apartments, Part II
Points about what Apartment is:
An apartment is a concurrency boundary; it’s an imaginary box drawn around objects and client threads that separates COM clients and COM objects that have incompatible threading characteristics.
Every thread that uses COM, and every object that those threads create, is assigned to an apartment.
When a thread calls COM’s CoInitialize or CoInitializeEx function, that thread is placed in an apartment. And when an object is created, it too is placed in an apartment.
Whenever it creates a new apartment, COM allocates an apartment object on the heap and initializes it with important information such as the apartment ID and apartment type. When it assigns a thread to an apartment, COM records the address of the corresponding apartment object in thread-local storage (TLS).

Here is another answer: Side note: If you are using some PowerShell 2.0 snap-ins, you need to launch PowerShell version 3 or greater with -MTA option to use them. PowerShell 2 apartment model is MTA versus later versions use STA as default. Other point is bitness. Normal calls in apartment are not marshalled (direct calls), so if your caller is x64 then callee must be also x64. Only way around this is to use remote procedure call (RPC), which add huge amount of overhead (spawn a new 32-bit process to load snap-in DLL and query result by some means).For developer: always publish type library - it make your COM object discovery and usage much easier! Every interface should be public and unique - implementation can be proprietary or open source.
Another situation
Example:
IStorage_vtbl** reference; // you got it by some means of factory
public unsafe int OpenStorage(char* pwcsName, IStorage pstgPriority, uint grfMode, char** snbExclude, uint reserved, IStorage* ppstg)
IStorage_vtbl** @this = (IStorage_vtbl**)reference;
IStorage_vtbl* vtbl = *@this;
if (vtbl == null)
throw new InvalidComObjectException();
Delegate genericDelegate = Marshal.GetDelegateForFunctionPointer(vtbl-&gt;method_6, typeof(delegate_6));
delegate_6 method = (delegate_6)genericDelegate;
return method(@this, pwcsName, pstgPriority, grfMode, snbExclude, reserved, ppstg);
This fragment of code just add 'this' pointer of instance for real call to COM subsystem
So, is this call to open instance of IStorage  STA or MTA?

Here is another answer: I find the existing explanations too gobbledygook. Here's my explanation in plain English:
If a thread creates a COM object that's set to STA (when calling CoCreateXXX you can pass a flag that sets the COM object to STA mode), then only this thread can access this COM object (that's what STA means - Single Threaded Apartment), other thread trying to call methods on this COM object is under the hood silently turned into delivering messages to the thread that creates(owns) the COM object. This is very much like the fact that only the thread that created a UI control can access it directly. And this mechanism is meant to prevent complicated lock/unlock operations.
If a thread creates a COM object that's set to MTA, then pretty much every thread can directly call methods on it.
That's pretty much the gist of it. Although technically there're some details I didn't mention, such as in the 'STA' paragraph, the creator thread must itself be STA. But this is pretty much all you have to know to understand STA/MTA/NA.

Here is another answer: As my understanding, the 'Apartment' is used to protect the COM objects from multi-threading issues.
If a COM object is not thread-safe, it should declare it as a STA object. Then only the thread who creates it can access it. The creation thread should declare itself as a STA thread. Under the hood, the thread stores the STA information in its TLS(Thread Local Storage). We call this behavior as that the thread enters a STA apartment. When other threads want to access this COM object, it should marshal the access to the creation thread. Basically, the creation thread uses messages mechanism to process the in-bound calls.
If a COM object is thread-safe, it should declare it as a MTA object. The MTA object can be accessed by multi-threads.

Here is another answer: It's all down to how calls to objects are handled, and how much protection they need. COM objects can ask the runtime to protect them against being called by multiple threads at the same time; those that don't can potentially be called concurrently from different threads, so they have to protect their own data.
In addition, it's also necessary for the runtime to prevent a COM object call from blocking the user interface, if a call is made from a user interface thread.
An apartment is a place for objects to live, and they contain one or more threads. The apartment defines what happens when calls are made. Calls to objects in an apartment will be received and processed on any thread in that apartment, with the exception that a call by a thread already in the right apartment is processed by itself (i.e. a direct call to the object).
Threads can be either in a Single-Threaded Apartment (in which case they are the only thread in that apartment) or in a Multi-Threaded Apartment. They specify which when the thread initializes COM for that thread.
The STA is primarily for compatibility with the user interface, which is tied to a specific thread. An STA receives notifications of calls to process by receiving a window message to a hidden window; when it makes an outbound call, it starts a modal message loop to prevent other window messages being processed. You can specify a message filter to be called, so that your application can respond to other messages.
By contrast all MTA threads share a single MTA for the process. COM may start a new worker thread to handle an incoming call if no threads are available, up to a pool limit. Threads making outbound calls simply block.
For simplicity we'll consider only objects implemented in DLLs, which advertise in the registry what they support, by setting the ThreadingModel value for their class's key. There are four options:
Main thread (ThreadingModel value not present). The object is created on the host's main UI thread, and all calls are marshalled to that thread. The class factory will only be called on that thread.
Apartment. This indicates that the class can run on any single-threaded-mode thread. If the thread that creates it is an STA thread, the object will run on that thread, otherwise it will be created in the main STA - if no main STA exists, an STA thread will be created for it. (This means MTA threads that create Apartment objects will be marshalling all calls to a different thread.) The class factory can be called concurrently by multiple STA threads so it must protect its internal data against this.
Free. This indicates a class designed to run in the MTA. It will always load in the MTA, even if created by an STA thread, which again means the STA thread's calls will be marshalled. This is because a Free object is generally written with the expectation that it can block.
Both. These classes are flexible and load in whichever apartment they're created from. They must be written to fit both sets of requirements, however: they must protect their internal state against concurrent calls, in case they're loaded in the MTA, but must not block, in case they're loaded in an STA.
From the .NET Framework, basically just use [STAThread] on any thread that creates UI. Worker threads should use the MTA, unless they're going to use Apartment-marked COM components, in which case use the STA to avoid marshalling overhead and scalability problems if the same component is called from multiple threads (as each thread will have to wait for the component in turn). It's much easier all around if you use a separate COM object per thread, whether the component is in the STA or MTA.
Comment for this answer: @Yogurtu: Why are you worried about the COM threading model at all? The STA/MTA decision is only relevant if you're using COM objects in your code. You cannot use MTA for the UI - .NET's internals are not intended to be used that way. If your animation stops, that's because you stopped pumping messages on your UI thread. Move long-running operations to a BackgroundWorker or split into small steps. Work needs to take < 16ms to keep smooth 60Hz animation!
Comment for this answer: I like your last conclusion, but regarding that, what should I do if I want on my UI to add a UserControl that the only thing that does is to reproduce a gif (like a loader)... I´m having troubles with this, the gif doesn´t spin if they are in the same thread... and I´m not sure if MTA on UI is a great idea, what would you do?
Comment for this answer: What's the difference between "apartment" and appdomain?

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 9, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2008:00:09/24/08', 'FavoriteCount': 158.0, 'PostTypeId': 1, 'Tags': '<.net><multithreading><com><apartments>', 'Title': 'Could you explain STA and MTA?', 'ViewCount': 156797, 'id': 127188, 'user_AboutMe': '', 'user_AccountId': 10346.0, 'user_DisplayName': 'John', 'user_Reputation': 5351.0, 'user_Views': 534.0, 'user_id': 19490.0}}