Sdílet prostřednictvím


Thread.SetApartmentState(ApartmentState) Metoda

Definice

Nastaví stav objektu apartment vlákna před jeho spuštěním.

public:
 void SetApartmentState(System::Threading::ApartmentState state);
[System.Runtime.Versioning.SupportedOSPlatform("windows")]
public void SetApartmentState (System.Threading.ApartmentState state);
public void SetApartmentState (System.Threading.ApartmentState state);
[<System.Runtime.Versioning.SupportedOSPlatform("windows")>]
member this.SetApartmentState : System.Threading.ApartmentState -> unit
member this.SetApartmentState : System.Threading.ApartmentState -> unit
Public Sub SetApartmentState (state As ApartmentState)

Parametry

state
ApartmentState

Nový stav objektu apartment.

Atributy

Výjimky

Pouze .NET Core a .NET 5 +: ve všech případech na macOS a Linux.

state není platným stavem typu apartment.

Vlákno již bylo spuštěno.

Stav objektu Apartment již byl inicializován.

Příklady

Následující příklad kódu ukazuje GetApartmentState SetApartmentState metody, a TrySetApartmentState . Příklad kódu vytvoří vlákno. Před spuštěním vlákna GetApartmentState zobrazí počáteční ApartmentState.Unknown stav a SetApartmentState změní stav na ApartmentState.STA . TrySetApartmentStateMetoda se pak vrátí false při pokusu o změnu stavu na, ApartmentState.MTA protože stav objektu Apartment je již nastaven. V případě, že došlo k pokusu o provedení stejné operace s SetApartmentState , by došlo k InvalidOperationException vyvolání.

Po spuštění vlákna se TrySetApartmentState znovu použije metoda. Tentokrát to vyvolá, ThreadStateException protože vlákno již bylo spuštěno.

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.

Poznámky

Nová vlákna jsou inicializována, jako ApartmentState.MTA kdyby jejich stav objektu Apartment nebyl nastaven předtím, než byly spuštěny. Před spuštěním vlákna musí být nastaven stav bytu.

Poznámka

Hlavní vlákno aplikace je ApartmentState.MTA ve výchozím nastavení inicializováno. Jediným způsobem, jak nastavit stav objektu Apartment hlavního vlákna aplikace na, ApartmentState.STA je použít STAThreadAttribute atribut na metodu vstupního bodu.

SetApartmentStateMetoda, společně s GetApartmentState metodou a TrySetApartmentState metodou, nahradí ApartmentState vlastnost.

Platí pro