Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>.NET objects are not bound to an apartment. Apartments only really come into play when your .NET code interacts with COM objects, though this is not hard to do unknowingly. For example, if you interact with the Windows clipboard, you are activating the COM subsystem. Apartments are set at the thread level, and once your thread has established its apartment (MTA, STA, etc.) it cannot change, so it is very important to get the apartment correct. All WinForms applications have [STAThread] attached to their Main() method to ensure the main application thread (i.e. the GUI message pumping thread) is STA and can interact with those convenient facilities like the Windows clipboard.</p> <p>The .NET runtime must be aware of the COM subsystem to provide COM interop functionality. It is pretty rare though to need the inner gruesome details.</p> <p>From what I understand, one of the goals of dividing COM objects into different apartments was so that applications could use software components developed with different threading models without having to be concerned with such issues themselves.</p> <p>There are a lot of complex issues involved here, particularly the notion that single threaded libraries are often not built to deal with re-entrancy and certainly do not support the notion of having two threads of execution modifying the object simultaneously.</p> <p>For example, suppose you want to use a COM object in your new multi-threaded C++ application. This COM object was developed in Visual Basic and is not thread safe, but you want to use multiple instances of that component from multiple threads. While it is true that you could add some locking mechanism to prevent improper access, the fact that the object exists in its own Single Thread Apartment (STA) means that the COM subsystem will serialize access to the object on your behalf. You don't have to know the threading model of the COM object to use it, or at least that is the way it usually works.</p> <p>.NET was written for our new multi-threaded world, and as such, does not use the concepts of apartments. The runtime is definitely aware of them, and has to be, to support interop with legacy COM objects.</p> <p>Apartments are very complex and lots of weird things can go wrong if you do not know what you are doing. Be glad you do not generally need to worry about it in .NET.</p> <p>If you want some really hard core information, you might check out <a href="http://blogs.msdn.com/cbrumme/archive/2004/02/02/66219.aspx" rel="nofollow noreferrer">Apartments and Pumping in the CLR</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload