Recently in the resolution of the problem had to use the thread, so as to contact the STA and MTA, degrees Niang interpretation mostly vague. Read more clearly on Wikipedia, taking advantage of the easy-to-access gap between sharing and self-improvement.
Apartment Type |
Description |
single-threaded Apartment (STA), (threadingmodel=Apartment) |
A Single thread was dedicated to execute the methods of the object. In such a arrangement, method calls from threads outside of the apartment is marshalled and automatically queued by the System (via a standard Windows message queue). Thus, the COM Run-time provides automatic synchronization to ensure the all method call of an object are always executed To completion before another is invoked. The developer therefore does not need to worry about thread locking or race conditions. |
multi-threaded Apartment [+] (MTA), (threadingmodel=free) |
The COM run-time provides no synchronization, and multiple threads is allowed to call COM objects simultaneously. COM objects therefore need to perform their own synchronization to prevent simultaneous access from multiple threads from Causing a race condition. Calls to the MTA object from a thread in an STA is also marshalled. |
Dynamically determined Apartment (threadingmodel=Both) |
In the Both apartment mode, the server auto-selects STA or MTA at object creation to match the apartment type of The calling thread. This can is useful to avoid marshaling overhead when MTA servers is accessed by a STA thread. |
Thread Neutral Apartment (NA), (threadingmodel=Neutral) |
A Special apartment without any assigned threads. When a STA or MTA thread calls a NA object in the same process and then the calling thread temporarily leaves its apartment a ND executes code directly in the NA without any thread switching. [Therefore], one can think of NA as an optimization for efficient interapartment method calls. |
The main idea is that single-threaded unit access externally is serial, and does not perform the next task until the previous task is fully completed (an object was always executed to completion before another is invoked), Developers generally do not have to worry about competition and deadlock issues (the developer therefore does not need to worry about thread locking or race conditions.). Chinese notes are here.
What is a single-threaded apartment (STA) What is a multithreaded apartment (MTA)