Thread.SetApartmentState(ApartmentState) Thread.SetApartmentState(ApartmentState) Thread.SetApartmentState(ApartmentState) Thread.SetApartmentState(ApartmentState) Method

Définition

Définit l'état de cloisonnement d'un thread avant qu'il ne soit démarré.Sets the apartment state of a thread before it is started.

public:
 void SetApartmentState(System::Threading::ApartmentState state);
public void SetApartmentState (System.Threading.ApartmentState state);
member this.SetApartmentState : System.Threading.ApartmentState -> unit
Public Sub SetApartmentState (state As ApartmentState)

Paramètres

state
ApartmentState ApartmentState ApartmentState ApartmentState

Nouvel état de cloisonnement.The new apartment state.

Exceptions

.NET Core uniquement : ce membre n’est pas pris en charge sur les plateformes macOS et Linux..NET Core only: This member is not supported on the macOS and Linux platforms.

state n’est pas un état de cloisonnement valide.state is not a valid apartment state.

Le thread a déjà été démarré.The thread has already been started.

L’état de cloisonnement a déjà été initialisé.The apartment state has already been initialized.

Exemples

L’exemple de code suivant illustre les méthodes GetApartmentState, SetApartmentState et TrySetApartmentState.The following code example demonstrates the GetApartmentState, SetApartmentState, and TrySetApartmentState methods. L’exemple de code crée un thread.The code example creates a thread. Avant le démarrage du thread, GetApartmentState affiche l’état initial ApartmentState.Unknown et SetApartmentState remplace l’État par ApartmentState.STA.Before the thread is started, GetApartmentState displays the initial ApartmentState.Unknown state and SetApartmentState changes the state to ApartmentState.STA. La méthode TrySetApartmentState retourne ensuite false lors de la tentative de modification de l’État en ApartmentState.MTA, car l’état de cloisonnement est déjà défini.The TrySetApartmentState method then returns false when attempting to change the state to ApartmentState.MTA because the apartment state is already set. Si la même opération a été tentée avec SetApartmentState, InvalidOperationException aurait été levée.If the same operation had been attempted with SetApartmentState, InvalidOperationException would have been thrown.

Une fois le thread démarré, la méthode TrySetApartmentState est réutilisée.After the thread is started, the TrySetApartmentState method is used again. Cette fois-ci, il lève ThreadStateException, car le thread a déjà été démarré.This time it throws ThreadStateException because the thread has already been started.

using namespace System;
using namespace System::Threading;

void ThreadProc()
{
    Thread::Sleep(2000);
};

void main()
{
    Thread^ t = gcnew Thread(gcnew ThreadStart(ThreadProc));
    Console::WriteLine("Before setting apartment state: {0}", 
            t->GetApartmentState());

    t->SetApartmentState(ApartmentState::STA);
    Console::WriteLine("After setting apartment state: {0}", 
        t->GetApartmentState());

    bool result = t->TrySetApartmentState(ApartmentState::MTA);
    Console::WriteLine("Try to change state: {0}", result);

    t->Start();

    Thread::Sleep(500);

    try
    {
        t->TrySetApartmentState(ApartmentState::STA);
    }
    catch (ThreadStateException^)
    {
        Console::WriteLine("ThreadStateException occurs " +
            "if apartment state is set after starting thread.");
    }

    t->Join();
}

/* This code example produces the following output:

Before setting apartment state: Unknown
After setting apartment state: STA
Try to change state: False
ThreadStateException occurs if apartment state is set after starting thread.
 */
using System;
using System.Threading;

class Example
{
    public static void Main()
    {
        Thread t = new Thread(ThreadProc);
        Console.WriteLine("Before setting apartment state: {0}", 
            t.GetApartmentState());

        t.SetApartmentState(ApartmentState.STA);
        Console.WriteLine("After setting apartment state: {0}", 
            t.GetApartmentState());

        bool result = t.TrySetApartmentState(ApartmentState.MTA);
        Console.WriteLine("Try to change state: {0}", result);

        t.Start();

        Thread.Sleep(500);

        try
        {
            t.TrySetApartmentState(ApartmentState.STA);
        }
        catch (ThreadStateException)
        {
            Console.WriteLine("ThreadStateException occurs " +
                "if apartment state is set after starting thread.");
        }

        t.Join();
    }

    public static void ThreadProc()
    {
        Thread.Sleep(2000);
    }
}

/* This code example produces the following output:

Before setting apartment state: Unknown
After setting apartment state: STA
Try to change state: False
ThreadStateException occurs if apartment state is set after starting thread.
 */
Imports System.Threading

Module Example

    Sub Main()
 
        Dim t As New Thread(AddressOf ThreadProc)
        Console.WriteLine("Before setting apartment state: {0}", _
            t.GetApartmentState())

        t.SetApartmentState(ApartmentState.STA)
        Console.WriteLine("After setting apartment state: {0}", _
            t.GetApartmentState())

        Dim result As Boolean = _
            t.TrySetApartmentState(ApartmentState.MTA)
        Console.WriteLine("Try to change state: {0}", result)

        t.Start()

        Thread.Sleep(500)

        Try
            t.TrySetApartmentState(ApartmentState.STA)
        Catch ex As ThreadStateException
            Console.WriteLine("ThreadStateException occurs " & _
                "if apartment state is set after starting thread.")
        End Try

        t.Join()
    End Sub

    Sub ThreadProc()
        Thread.Sleep(2000)
    End Sub
End Module

' This code example produces the following output:
'
'Before setting apartment state: Unknown
'After setting apartment state: STA
'Try to change state: False
'ThreadStateException occurs if apartment state is set after starting thread.

Remarques

Les nouveaux threads sont initialisés en tant que ApartmentState.MTA si leur état de cloisonnement n’a pas été défini avant de démarrer.New threads are initialized as ApartmentState.MTA if their apartment state has not been set before they are started. L’état de cloisonnement doit être défini avant le démarrage d’un thread.Apartment state must be set before a thread is started.

Notes

Le thread d’application principal est initialisé à ApartmentState.MTA par défaut.The main application thread is initialized to ApartmentState.MTA by default. La seule façon de définir l’état de cloisonnement du thread d’application principal sur ApartmentState.STA consiste à appliquer l’attribut STAThreadAttribute à la méthode de point d’entrée.The only way to set the apartment state of the main application thread to ApartmentState.STA is to apply the STAThreadAttribute attribute to the entry point method.

La méthode SetApartmentState, ainsi que la méthode GetApartmentState et la méthode TrySetApartmentState, remplacent la propriété ApartmentState.The SetApartmentState method, along with the GetApartmentState method and the TrySetApartmentState method, replaces the ApartmentState property.

S’applique à