Share via


ApartmentState 枚举

指定 Thread 的单元状态。

**命名空间:**System.Threading
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Enumeration ApartmentState
用法
Dim instance As ApartmentState
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum ApartmentState
[SerializableAttribute] 
[ComVisibleAttribute(true)] 
public enum class ApartmentState
/** @attribute SerializableAttribute() */ 
/** @attribute ComVisibleAttribute(true) */ 
public enum ApartmentState
SerializableAttribute 
ComVisibleAttribute(true) 
public enum ApartmentState

成员

  成员名称 说明
由 .NET Compact Framework 支持 MTA Thread 将创建并进入一个多线程单元。 
由 .NET Compact Framework 支持 STA Thread 将创建并进入一个单线程单元。 
由 .NET Compact Framework 支持 Unknown 尚未设置 ApartmentState 属性。 

备注

单元是进程内部具有相同线程访问要求的对象的逻辑容器。同一单元中的所有对象都可以接收从该单元中的任何线程发出的调用。.NET Framework 不使用单元,托管对象自己负责以线程安全的方式使用所有共享资源。

由于 COM 类使用单元,因此公共语言运行库需要在 COM interop 的情况下调用 COM 对象时创建并初始化一个单元。托管线程可以创建并进入只允许有一个线程的单线程单元 (STA) 或者包含一个或多个线程的多线程单元 (MTA)。通过将线程的 ApartmentState 属性设置为 ApartmentState 枚举值之一,可以控制所创建的单元的类型。由于给定线程只能初始化 COM 单元一次,因此在第一次调用非托管代码之后就不能更改单元类型。

有关更多信息,请参见 Thread托管和非托管线程处理高级 COM 互操作性

示例

下面的代码示例阐释了如何设置线程的单元状态。

Imports Microsoft.VisualBasic
Imports System
Imports System.Threading

Public Class ApartmentTest

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

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

        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
using System;
using System.Threading;

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

        // The following line is ignored since 
        // ApartmentState can only be set once.
        newThread.SetApartmentState(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.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);
    }
}
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);
   
   // The following line is ignored since 
   // ApartmentState can only be set once.
   newThread->SetApartmentState(ApartmentState::STA);
   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() );
   }

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

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

        newThread.set_ApartmentState(ApartmentState.MTA);

        // The following line is ignored since 
        // ApartmentState can only be set once.
        newThread.set_ApartmentState(ApartmentState.STA);
        Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                        newThread.get_ThreadState(),
                        newThread.get_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.set_ApartmentState(ApartmentState.STA);
        }
        catch (ThreadStateException stateException) {
            Console.WriteLine("\n{0} caught:\n" + 
                "Thread is not in the Unstarted or Running state.", 
                stateException.GetType().get_Name());
            Console.WriteLine("ThreadState: {0}, ApartmentState: {1}",
                newThread.get_ThreadState(),newThread.get_ApartmentState());
        }
    } //main

    static void ThreadMethod()
    {
        Thread.Sleep(1000);
    } //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

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0

请参见

参考

System.Threading 命名空间
Thread

其他资源

托管和非托管线程处理
高级 COM 互操作性