ApartmentState ApartmentState ApartmentState ApartmentState Enum

Definition

Gibt den Apartmentzustand eines Thread an.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
Vererbung
ApartmentStateApartmentStateApartmentStateApartmentState
Attribute

Felder

MTA MTA MTA MTA 1

Der Thread erstellt ein MTA (Multithreaded Apartment) und wird in dieses aufgenommen.The Thread will create and enter a multithreaded apartment.

STA STA STA STA 0

Der Thread erstellt ein STA (Singlethreaded Apartment) und wird in dieses aufgenommen.The Thread will create and enter a single-threaded apartment.

Unknown Unknown Unknown Unknown 2

Die ApartmentState-Eigenschaft wurde nicht festgelegt.The ApartmentState property has not been set.

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

Ein Apartment ist ein logischer Container innerhalb eines Prozesses für-Objekte, die dieselben Thread Zugriffs Anforderungen nutzen.An apartment is a logical container within a process for objects sharing the same thread access requirements. Alle Objekte im selben Apartment können Aufrufe von einem beliebigen Thread im Apartment empfangen.All objects in the same apartment can receive calls from any thread in the apartment. Der .NET Framework verwendet keine-Apartments, und verwaltete Objekte sind dafür verantwortlich, alle freigegebenen Ressourcen auf Thread sichere Weise zu verwenden.The .NET Framework does not use apartments, and managed objects are responsible for using all shared resources in a thread-safe manner themselves.

Da com-Klassen Apartments verwenden, muss das Common Language Runtime ein Apartment erstellen und initialisieren, wenn ein COM-Objekt in einer COM-Interop-Situation aufgerufen wird.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. Ein verwalteter Thread kann ein Single Thread-Apartment (STA) erstellen und eingeben, das nur einen Thread zulässt, oder ein Multithreadapartment (MTA), das einen oder mehrere Threads enthält.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. Sie können den Typ des erstellten Apartment steuern, indem Sie ApartmentState die-Eigenschaft des Threads auf einen der Werte ApartmentState der-Enumeration festlegen.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. Da ein angegebener Thread nur ein com-Apartment einmalig initialisieren kann, können Sie den Apartment-Typ nach dem ersten nicht verwalteten Code nicht ändern.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.

Weitere Informationen finden ThreadSie unter, verwaltetes und nicht verwaltetes Threadingund Erweiterte COM-Interoperabilität.For more information, see Thread, Managed and Unmanaged Threading, and Advanced COM Interoperability.

Gilt für:

Siehe auch