Thread.ApartmentState Thread.ApartmentState Thread.ApartmentState Thread.ApartmentState Property

Definition

Warnung

Diese API ist jetzt veraltet.

Ruft den Apartmentzustand dieses Threads ab oder legt diesen fest.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

Eigenschaftswert

Einer der ApartmentState-Werte.One of the ApartmentState values. Der Anfangswert ist Unknown.The initial value is Unknown.

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).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)).

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie der Apartment Zustand eines Threads festgelegt wird.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

Hinweise

Die ApartmentState-Eigenschaft ist veraltet.The ApartmentState property is obsolete. Die nicht veralteten Alternativen sind die GetApartmentState-Methode zum Abrufen des Apartment Zustands und die SetApartmentState-Methode, um den Apartment Zustand festzulegen.The non-obsolete alternatives are the GetApartmentState method to retrieve the apartment state and the SetApartmentState method to set the apartment state.

In den .NET Framework Versionen 1,0 und 1,1 markiert die Eigenschaft ApartmentState einen Thread, um anzugeben, dass er in einem Single Thread-oder Multithread-Apartment ausgeführt wird.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. Diese Eigenschaft kann festgelegt werden, wenn sich der Thread im Unstarted-oder Running-Thread Zustand befindet. Sie kann jedoch nur einmal für einen Thread festgelegt werden.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. Wenn die Eigenschaft nicht festgelegt wurde, wird Unknown zurückgegeben.If the property has not been set, it returns Unknown.

Der Versuch, die ApartmentState-Eigenschaft zu verwenden, um den Apartment Zustand eines Threads festzulegen, dessen Apartment Zustand bereits festgelegt wurde, wird ignoriert.An attempt to use the ApartmentState property to set the apartment state of a thread whose apartment state has already been set is ignored. Die SetApartmentState-Methode löst in diesem Fall jedoch eine InvalidOperationException aus.However, the SetApartmentState method throws a InvalidOperationException in this case.

Wichtig

In der .NET Framework Version 2,0 werden neue Threads als ApartmentState.MTA initialisiert, wenn der Apartment Zustand nicht festgelegt wurde, bevor Sie gestartet werden.In 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. Der Hauptanwendungs Thread wird standardmäßig mit ApartmentState.MTA initialisiert.The main application thread is initialized to ApartmentState.MTA by default. Der Hauptanwendungs Thread kann nicht mehr auf ApartmentState.STA festgelegt werden, indem die System.Threading.ApartmentState-Eigenschaft in der ersten Codezeile festgelegt wird.You can no longer set the main application thread to ApartmentState.STA by setting the System.Threading.ApartmentState property on the first line of code. Verwenden Sie stattdessen den STAThreadAttribute.Use the STAThreadAttribute instead.

In der .NET Framework Version 2,0 können Sie das COM-Threading Modell für eine C++ Anwendung mithilfe der /CLRTHREADATTRIBUTE (Set CLR Thread Attribute) -Linkeroption angeben.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.

Gilt für:

Siehe auch