ApartmentState Výčet

Definice

Určuje stav objektu Apartment Thread.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
Dědičnost
ApartmentState
Atributy

Pole

MTA 1

Thread vytvoří a vstoupí do vícevláknového objektu apartment.The Thread will create and enter a multithreaded apartment.

STA 0

Thread vytvoří a vstoupí v jednom vlákně Apartment.The Thread will create and enter a single-threaded apartment.

Unknown 2

Vlastnost ApartmentState nebyla nastavena.The ApartmentState property has not been set.

Příklady

Následující příklad kódu ukazuje, jak nastavit stav objektu apartment vlákna.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

Poznámky

Apartment je logický kontejner v rámci procesu pro objekty sdílející stejné požadavky na přístup k vláknům.An apartment is a logical container within a process for objects sharing the same thread access requirements. Všechny objekty ve stejném izolovaném prostoru můžou přijímat volání z libovolného vlákna v izolovaném prostoru.All objects in the same apartment can receive calls from any thread in the apartment. .NET Framework nepoužívá objekty Apartment a spravované objekty jsou zodpovědné za použití všech sdílených prostředků v samotném vlákně bezpečném pro přístup z více vláken.The .NET Framework does not use apartments, and managed objects are responsible for using all shared resources in a thread-safe manner themselves.

Vzhledem k tomu, že třídy COM používají objekty Apartment, modul CLR (Common Language Runtime) potřebuje při volání objektu COM v případě interoperability modelu COM vytvořit a inicializovat objekt Apartment.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. Spravované vlákno může vytvořit a zadat vícevláknové prostředí Apartment (STA), které umožňuje pouze jedno vlákno, nebo vícevláknové prostředí (MTA) obsahující jeden nebo více vláken.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. Můžete ovládat typ objektu Apartment vytvořen nastavením vlastnosti ApartmentState vlákna na jednu z hodnot výčtu ApartmentState.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. Vzhledem k tomu, že dané vlákno může inicializovat pouze objekt Apartment modelu COM, nelze po prvním volání nespravovaného kódu změnit typ objektu apartment.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.

Další informace najdete v tématu Thread, spravované a nespravované zřetězenía pokročilou interoperabilitu modelu COM.For more information, see Thread, Managed and Unmanaged Threading, and Advanced COM Interoperability.

Platí pro

Viz také