Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

ApartmentState Enumeration

Specifies the apartment state of a Thread.

[Visual Basic]
<Serializable>
Public Enum ApartmentState
[C#]
[Serializable]
public enum ApartmentState
[C++]
[Serializable]
__value public enum ApartmentState
[JScript]
public
   Serializable
enum ApartmentState

Remarks

An apartment is a logical container within a process for objects sharing the same thread access requirements. All objects in the same apartment can receive calls from any thread in the apartment. The .NET Framework does not use apartments, and managed objects are responsible for using all shared resources in a thread-safe manner themselves.

Because COM classes use apartments, the common language runtime needs to create and initialize an apartment when calling a COM object in a COM interop situation. A managed thread can create and enter a single-threaded apartment (STA) that allows only one thread, or a multithreaded apartment (MTA) that contains one or more threads. You can control the type of apartment created by setting the ApartmentState property of the thread to one of the values of the ApartmentState enumeration. Because a given thread can only initialize a COM apartment once, you cannot change the apartment type after the first call to the unmanaged code.

For more information, see Thread, Managed and Unmanaged Threading, and Advanced COM Interop.

Members

Member name Description
MTA The Thread will create and enter a multithreaded apartment.
STA The Thread will create and enter a single-threaded apartment.
Unknown The ApartmentState property has not been set.

Example

[Visual Basic, C#, C++] The following code example demonstrates how to set the apartment state of a thread.

[Visual Basic] 
Imports Microsoft.VisualBasic
Imports System
Imports System.Threading

Public Class ApartmentTest

    Shared Sub Main()
    
        Dim newThread As Thread = New Thread(AddressOf ThreadMethod)
        newThread.ApartmentState = ApartmentState.MTA

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

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

        newThread.Start()

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

    End Sub

    Shared Sub ThreadMethod()
        Thread.Sleep(1000)
    End Sub

End Class

[C#] 
using System;
using System.Threading;

class ApartmentTest
{
    static void Main()
    {
        Thread newThread = 
            new Thread(new ThreadStart(ThreadMethod));
        newThread.ApartmentState = ApartmentState.MTA;

        // The following line is ignored since 
        // ApartmentState can only be set once.
        newThread.ApartmentState = ApartmentState.STA;

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

        newThread.Start();

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

    static void ThreadMethod()
    {
        Thread.Sleep(1000);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Threading;

__gc class ApartmentTest
{
public :
    static void ThreadMethod()
    {
        Thread::Sleep(1000);
    }
};

void main()
{
    Thread* newThread = 
        new Thread(new ThreadStart(0, &ApartmentTest::ThreadMethod));
        newThread->ApartmentState = ApartmentState::MTA;

        // The following line is ignored since 
        // ApartmentState can only be set once.
        newThread->ApartmentState = ApartmentState::STA;

        Console::WriteLine(S"ThreadState: {0}, ApartmentState: {1}", 
            __box(newThread->ThreadState)->ToString(),
            __box(newThread->ApartmentState)->ToString());

    newThread->Start();

    // Wait for newThread to start and go to sleep.
    Thread::Sleep(300);
    try
    {
        // This causes an exception since newThread is sleeping.
        newThread->ApartmentState = ApartmentState::STA;
    }
    catch(ThreadStateException* stateException)
    {
        Console::WriteLine(S"\n{0} caught:\n"
            S"Thread is not in the Unstarted or Running state.",
            stateException->GetType()->Name);
        Console::WriteLine(S"ThreadState: {0}, ApartmentState: {1}", 
            __box(newThread->ThreadState)->ToString(),
            __box(newThread->ApartmentState)->ToString());
    }
}

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Threading

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.Threading Namespace | Thread | Managed and Unmanaged Threading | Advanced COM Interop

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft