Thread.ApartmentState Property

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 Property ApartmentState As ApartmentState

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.

Imports System.Threading

Public Class ApartmentTest

    <MTAThread> _
    Shared Sub Main()

        Dim newThread As Thread = New Thread(AddressOf ThreadMethod)

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


        ' Wait for newThread to start and go to sleep.
            ' This causes an exception since newThread is sleeping.
        Catch stateException As ThreadStateException
            Console.WriteLine(vbCrLf & "{0} caught:" & vbCrLf & _
                "Thread is not In the Unstarted or Running state.", _
            Console.WriteLine("ThreadState: {0}, ApartmentState: " & _
                "{1}", newThread.ThreadState, newThread.GetApartmentState())
        End Try

    End Sub

    Shared Sub ThreadMethod()
    End Sub

End Class

.NET Framework
Available since 1.1
Return to top