ApartmentState ApartmentState ApartmentState ApartmentState Enum

定义

指定 Thread 的单元状态。Specifies the apartment state of a Thread.

public enum class ApartmentState
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum ApartmentState
type ApartmentState = 
Public Enum ApartmentState
继承
ApartmentStateApartmentStateApartmentStateApartmentState
属性

字段

MTA MTA MTA MTA 1

Thread 将创建并进入一个多线程单元。The Thread will create and enter a multithreaded apartment.

STA STA STA STA 0

Thread 将创建并进入一个单线程单元。The Thread will create and enter a single-threaded apartment.

Unknown Unknown Unknown Unknown 2

尚未设置 ApartmentState 属性。The ApartmentState property has not been set.

示例

下面的代码示例演示如何设置线程的单元状态。The following code example demonstrates how to set the apartment state of a thread.

using namespace System;
using namespace System::Threading;
ref class ApartmentTest
{
public:
   static void ThreadMethod()
   {
      Thread::Sleep( 1000 );
   }

};

int main()
{
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( &ApartmentTest::ThreadMethod ) );
   newThread->SetApartmentState(ApartmentState::MTA);
   
   Console::WriteLine( "ThreadState: {0}, ApartmentState: {1}", newThread->ThreadState.ToString(), newThread->GetApartmentState().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->SetApartmentState(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.ToString(), newThread->GetApartmentState().ToString() );
   }

}

using System;
using System.Threading;

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

        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.SetApartmentState(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.GetApartmentState());
        }
    }

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

Public Class ApartmentTest

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

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

        newThread.Start()

        ' Wait for newThread to start and go to sleep.
        Thread.Sleep(300)
        Try
            ' This causes an exception since newThread is sleeping.
            newThread.SetApartmentState(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.GetApartmentState())
        End Try

    End Sub

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

End Class

注解

单元是进程内共享相同线程访问要求的对象的逻辑容器。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. .NET Framework 不使用单元, 而托管对象则负责以线程安全的方式自行使用所有共享资源。The .NET Framework does not use apartments, and managed objects are responsible for using all shared resources in a thread-safe manner themselves.

因为 COM 类使用单元, 所以公共语言运行时需要在 COM 互操作情况下调用 COM 对象时创建和初始化单元。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. 托管线程可以创建并输入单线程单元 (STA), 该单元只允许一个线程, 或包含一个或多个线程的多线程单元 (MTA)。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. 可以通过将线程的ApartmentState属性设置为ApartmentState枚举的值之一来控制创建的单元类型。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. 由于给定线程只能初始化一个 COM 单元一次, 因此不能在首次调用非托管代码后更改单元类型。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.

有关详细信息, 请Thread参阅、托管和非托管线程处理高级 COM 互操作性For more information, see Thread, Managed and Unmanaged Threading, and Advanced COM Interoperability.

适用于

另请参阅