Partager via


Thread.ApartmentState Propriété

Définition

Attention

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

Attention

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

Attention

Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.

Obtient ou définit l'état de cloisonnement de ce thread.

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("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")]
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
[<System.Obsolete("Deprecated in favor of GetApartmentState, SetApartmentState and TrySetApartmentState.")>]
member this.ApartmentState : System.Threading.ApartmentState with get, set
Public Property ApartmentState As ApartmentState

Valeur de propriété

ApartmentState

Une des valeurs de l'objet ApartmentState. La valeur initiale est Unknown.

Attributs

Exceptions

Une tentative est effectuée pour définir cette propriété sur un état qui n’est pas un état de cloisonnement valide (un état autre que celui de thread unique cloisonné (STA) ou de multithread cloisonné (MTA)).

Exemples

L’exemple de code suivant montre comment définir l’état de cloisonnement d’un 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.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);
    }
}
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

Remarques

La ApartmentState propriété est obsolète. Les alternatives non obsolètes sont la GetApartmentState méthode permettant de récupérer l’état de cloisonnement et la SetApartmentState méthode pour définir l’état de cloisonnement.

dans les versions 1,0 et 1,1 de .NET Framework, la ApartmentState propriété marque un thread pour indiquer qu’il s’exécutera dans un thread unique cloisonné ou multithread. Cette propriété peut être définie lorsque le thread est dans l' Unstarted Running État du thread ou ; toutefois, il ne peut être défini qu’une seule fois pour un thread. Si la propriété n’a pas été définie, elle retourne Unknown .

Une tentative d’utilisation de la ApartmentState propriété pour définir l’état de cloisonnement d’un thread dont l’état de cloisonnement a déjà été défini est ignorée. Toutefois, la SetApartmentState méthode lève une InvalidOperationException dans ce cas.

Important

dans .NET Framework version 2,0, les nouveaux threads sont initialisés comme ApartmentState.MTA si leur état de cloisonnement n’avait pas été défini avant de démarrer. Le thread d’application principal est initialisé ApartmentState.MTA par défaut. Vous ne pouvez plus définir le thread d’application principal ApartmentState.STA en définissant la System.Threading.ApartmentState propriété sur la première ligne de code. Utilisez à la STAThreadAttribute place.

dans .NET Framework version 2,0, vous pouvez spécifier le modèle de thread COM pour une application C++ à l’aide de l’option de l’éditeur de liens /clrthreadattribute (définir l’attribut de thread CLR) .

S’applique à

Voir aussi