Export (0) Print
Expand All

Thread.ApartmentState Property

NOTE: This property is now obsolete. The non-obsolete alternative is GetApartmentStateSetApartmentState.

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.", False)> _
Public Property ApartmentState As ApartmentState
Dim instance As Thread
Dim value As ApartmentState

value = instance.ApartmentState

instance.ApartmentState = value
/** @property */
public ApartmentState get_ApartmentState ()

/** @property */
public void set_ApartmentState (ApartmentState value)

public function get ApartmentState () : ApartmentState

public function set ApartmentState (value : ApartmentState)

Property Value

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

Exception typeCondition


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)).

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.


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 HostProtectionAttribute attribute applied to this property has the following Resources property value: Synchronization | SelfAffectingThreading. The HostProtectionAttribute does not affect desktop applications (which are typically started by double-clicking an icon, typing a command, or entering a URL in a browser). For more information, see the HostProtectionAttribute class or SQL Server Programming and Host Protection Attributes.

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

Imports Microsoft.VisualBasic
Imports System
Imports System.Threading

Public Class ApartmentTest

    <MTAThread> _
    Shared Sub Main()
        Dim newThread As Thread = New Thread(AddressOf ThreadMethod)

        ' The following line is ignored since 
        ' ApartmentState can only be set once.

        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

import System.*;
import System.Threading.*;
import System.Threading.Thread;    

class ApartmentTest
    public static void main(String[] args)
        Thread newThread = new Thread(new ThreadStart(ThreadMethod));


        // The following line is ignored since 
        // ApartmentState can only be set once.
        Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",

        // Wait for newThread to start and go to sleep.
        try {
            // 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}",
    } //main

    static void ThreadMethod()
    } //ThreadMethod
} //ApartmentTest

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 1.0, 1.1
Obsolete (compiler warning) in 2.0

Community Additions

© 2014 Microsoft