Thread.ApartmentState Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Note: This API is now obsolete.

Gets or sets the apartment state of this thread.

Namespace:   System.Threading
Assembly:  mscorlib (in mscorlib.dll)

[ObsoleteAttribute("The ApartmentState property has been deprecated.  Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", 
public ApartmentState ApartmentState {
	[HostProtectionAttribute(SecurityAction.LinkDemand, Synchronization = true, 
		SelfAffectingThreading = true)]

Property Value

Type: System.Threading.ApartmentState

One of the ApartmentState values. The initial value is Unknown.

Exception Condition

An attempt is made to set this property to a state that is not a valid apartment state (a state other than single-threaded apartment (STA) or multithreaded apartment (MTA)).

The ApartmentState property is obsolete. The non-obsolete alternatives are the GetApartmentState method to retrieve the apartment state and the SetApartmentState method to set the apartment state.

In the .NET Framework versions 1.0 and 1.1, the ApartmentState property marks a thread to indicate that it will execute in a single-threaded or multithreaded apartment. This property can be set when the thread is in the Unstarted or Running thread state; however, it can be set only once for a thread. If the property has not been set, it returns Unknown.

An attempt to use the ApartmentState property to set the apartment state of a thread whose apartment state has already been set is ignored. However, the SetApartmentState method throws a InvalidOperationException in this case.


In the .NET Framework version 2.0, new threads are initialized as ApartmentState.MTA if their apartment state has not been set before they are started. The main application thread is initialized to ApartmentState.MTA by default. You can no longer set the main application thread to ApartmentState.STA by setting the System.Threading.ApartmentState property on the first line of code. Use the STAThreadAttribute instead.

In the .NET Framework version 2.0, you can specify the COM threading model for a C++ application using the -CLRTHREADATTRIBUTE (Set CLR Thread Attribute) linker option.

The following code example demonstrates how to set the apartment state of a thread.

using System;
using System.Threading;

class ApartmentTest
    static void Main()
        Thread newThread = 
            new Thread(new ThreadStart(ThreadMethod));

        Console.WriteLine("ThreadState: {0}, ApartmentState: {1}", 
            newThread.ThreadState, newThread.ApartmentState);


        // Wait for newThread to start and go to sleep.
            // This causes an exception since newThread is sleeping.
        catch(ThreadStateException stateException)
            Console.WriteLine("\n{0} caught:\n" +
                "Thread is not in the Unstarted or Running state.", 
            Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                newThread.ThreadState, newThread.GetApartmentState());

    static void ThreadMethod()

.NET Framework
Available since 1.1
Return to top