Thread.ApartmentState 属性

定义

警告

此 API 现已过时。

获取或设置此线程的单元状态。Gets or sets the apartment state of this thread.

public:
 property System::Threading::ApartmentState ApartmentState { System::Threading::ApartmentState get(); void set(System::Threading::ApartmentState value); };
[System.Obsolete("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")]
[System.Obsolete("The ApartmentState property has been deprecated.  Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.", false)]
public System.Threading.ApartmentState ApartmentState { get; set; }
member this.ApartmentState : System.Threading.ApartmentState with get, set
Public Property ApartmentState As ApartmentState

属性值

ApartmentState 值之一。One of the ApartmentState values. 初始值为 UnknownThe initial value is Unknown.

属性

异常

尝试将此属性设置为不是有效单元状态的状态 (单线程单元 (STA) 或多线程单元 (MTA) 以外的状态)。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 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

注解

ApartmentState 属性已过时。The ApartmentState property is obsolete. 未过时的替代项是检索单元状态的 GetApartmentState 方法,以及 SetApartmentState 方法来设置单元状态。The non-obsolete alternatives are the GetApartmentState method to retrieve the apartment state and the SetApartmentState method to set the apartment state.

在 .NET Framework 版本1.0 和1.1 中,ApartmentState 属性标记一个线程以指示它将在单线程或多线程单元中执行。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. 当线程处于 UnstartedRunning 线程状态时,可以设置此属性;但对于线程,只能设置一次。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. 如果尚未设置属性,则它将返回 UnknownIf the property has not been set, it returns Unknown.

尝试使用 ApartmentState 属性设置已设置了单元状态的线程的单元状态。An attempt to use the ApartmentState property to set the apartment state of a thread whose apartment state has already been set is ignored. 但是,在这种情况下,SetApartmentState 方法会引发 InvalidOperationExceptionHowever, the SetApartmentState method throws a InvalidOperationException in this case.

重要

在 .NET Framework 版本2.0 中,如果在启动新线程之前未设置其单元状态,则将其初始化为 ApartmentState.MTAIn 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. 默认情况下,主应用程序线程初始化为 ApartmentState.MTAThe main application thread is initialized to ApartmentState.MTA by default. 你不能再通过设置第一行代码的 System.Threading.ApartmentState 属性来将主应用程序线程设置为 ApartmentState.STAYou can no longer set the main application thread to ApartmentState.STA by setting the System.Threading.ApartmentState property on the first line of code. 改用 STAThreadAttributeUse the STAThreadAttribute instead.

在 .NET Framework 版本2.0 中,可以使用C++ /CLRTHREADATTRIBUTE (设置 CLR 线程属性)链接器选项指定应用程序的 COM 线程模型。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.

适用于

另请参阅