Thread.ApartmentState Eigenschaft

Definition

Achtung

The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.

Achtung

The ApartmentState property has been deprecated. Use GetApartmentState, SetApartmentState or TrySetApartmentState instead.

Ruft den Apartmentzustand dieses Threads ab oder legt diesen fest.

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

Eigenschaftswert

Einer der ApartmentState-Werte. Der Anfangswert ist Unknown.

Attribute

Ausnahmen

Es wird versucht, diese Eigenschaft in einen Zustand zu versetzen, der kein gültiger Apartmentzustand ist, d. h. in einen anderen Zustand als Singlethread-Apartment (STA) oder Multithread-Apartment (MTA).

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie der Apartmentstatus eines Threads festgelegt wird.

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.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(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);
    }
}
open System.Threading

let threadMethod () = Thread.Sleep 1000

let newThread = Thread threadMethod
newThread.SetApartmentState ApartmentState.MTA

printfn $"ThreadState: {newThread.ThreadState}, ApartmentState: {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

with :? ThreadStateException as stateException ->
    printfn $"\n{stateException.GetType().Name} caught:\nThread is not in the Unstarted or Running state."
    printfn $"ThreadState: {newThread.ThreadState}, ApartmentState: {newThread.GetApartmentState()}"
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

Hinweise

Die ApartmentState-Eigenschaft ist veraltet. Die nicht veralteten Alternativen sind die GetApartmentState Methode zum Abrufen des Wohnungszustands und die SetApartmentState Methode zum Festlegen des Wohnungszustands.

In .NET Framework Versionen 1.0 und 1.1 markiert die ApartmentState -Eigenschaft einen Thread, um anzugeben, dass er in einem Singlethread- oder Multithread-Apartment ausgeführt wird. Diese Eigenschaft kann festgelegt werden, wenn sich der Thread im UnstartedRunning Oder-Zustand befindet. Sie kann jedoch nur einmal für einen Thread festgelegt werden. Wenn die -Eigenschaft nicht festgelegt wurde, wird zurückgegeben Unknown.

Ein Versuch, mit der ApartmentState -Eigenschaft den Apartmentstatus eines Threads festzulegen, dessen Apartmentstatus bereits festgelegt wurde, wird ignoriert. In diesem Fall löst die SetApartmentState -Methode jedoch ein aus InvalidOperationException .

Wichtig

In .NET Framework Version 2.0 werden neue Threads so initialisiert, als ApartmentState.MTA ob ihr Wohnungsstatus noch nicht festgelegt wurde, bevor sie gestartet werden. Der Hauptanwendungsthread wird standardmäßig mit ApartmentState.MTA initialisiert. Sie können den Hauptanwendungsthread nicht mehr auf festlegen, ApartmentState.STA indem Sie die System.Threading.ApartmentState Eigenschaft in der ersten Codezeile festlegen. Verwenden Sie stattdessen STAThreadAttribute.

In .NET Framework Version 2.0 können Sie das COM-Threadingmodell für eine C++-Anwendung mithilfe der Linkeroption /CLRTHREADATTRIBUTE (Set CLR Thread Attribute) angeben.

Gilt für:

Weitere Informationen