Thread.GetApartmentState Metoda

Definice

Vrací hodnotu ApartmentState určující stav objektu apartment.Returns an ApartmentState value indicating the apartment state.

public:
 System::Threading::ApartmentState GetApartmentState();
public System.Threading.ApartmentState GetApartmentState ();
member this.GetApartmentState : unit -> System.Threading.ApartmentState
Public Function GetApartmentState () As ApartmentState

Návraty

Jedna z hodnot ApartmentState označující stav objektu apartment spravovaného vlákna.One of the ApartmentState values indicating the apartment state of the managed thread. Výchozí hodnota je Unknown.The default is Unknown.

Příklady

Následující příklad kódu ukazuje metody GetApartmentState, SetApartmentStatea TrySetApartmentState.The following code example demonstrates the GetApartmentState, SetApartmentState, and TrySetApartmentState methods. Příklad kódu vytvoří vlákno.The code example creates a thread. Před zahájením vlákna GetApartmentState zobrazí počáteční stav ApartmentState.Unknown a SetApartmentState změní stav na ApartmentState.STA.Before the thread is started, GetApartmentState displays the initial ApartmentState.Unknown state and SetApartmentState changes the state to ApartmentState.STA. Metoda TrySetApartmentState pak vrátí false při pokusu o změnu stavu na ApartmentState.MTA, protože stav objektu Apartment již je nastaven.The TrySetApartmentState method then returns false when attempting to change the state to ApartmentState.MTA because the apartment state is already set. Pokud došlo k pokusu o provedení stejné operace s SetApartmentState, InvalidOperationException by byl vyvolán.If the same operation had been attempted with SetApartmentState, InvalidOperationException would have been thrown.

Po spuštění vlákna se znovu použije metoda TrySetApartmentState.After the thread is started, the TrySetApartmentState method is used again. Tentokrát to vyvolá ThreadStateException, protože vlákno již bylo spuštěno.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.

Poznámky

Tato metoda, společně s metodou SetApartmentState a metodou TrySetApartmentState, nahrazuje vlastnost ApartmentState.This method, along with the SetApartmentState method and the TrySetApartmentState method, replaces the ApartmentState property.

Platí pro