共用方式為


Thread.GetApartmentState 方法

定義

傳回表示 Apartment 狀態的 ApartmentState 值。

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

傳回

其中一個 ApartmentState 值,表示 Managed 執行緒的 Apartment 狀態。 預設為 Unknown

範例

下列程式碼範例示範 GetApartmentStateSetApartmentStateTrySetApartmentState 方法。 程式碼範例會建立執行緒。 線上程啟動之前, GetApartmentState 顯示初始 ApartmentState.Unknown 狀態,並將 SetApartmentState 狀態變更為 ApartmentState.STATrySetApartmentState方法接著會在嘗試將狀態 ApartmentState.MTA 變更為 時傳回 false ,因為已設定 Apartment 狀態。 如果嘗試使用 SetApartmentState 相同的作業, InvalidOperationException 則會擲回 。

執行緒啟動之後, TrySetApartmentState 會再次使用 方法。 這次會擲回 ThreadStateException ,因為執行緒已經啟動。

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.
 */
open System.Threading

let threadProc () = Thread.Sleep 2000

let t = Thread threadProc
printfn $"Before setting apartment state: {t.GetApartmentState()}"

t.SetApartmentState ApartmentState.STA
printfn $"After setting apartment state: {t.GetApartmentState()}"

let result = t.TrySetApartmentState ApartmentState.MTA
printfn $"Try to change state: {result}"

t.Start()

Thread.Sleep 500

try
    t.TrySetApartmentState ApartmentState.STA |> ignore
with :? ThreadStateException ->
    printfn "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.
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.

備註

這個方法以及 SetApartmentState 方法和 TrySetApartmentState 方法,會取代 ApartmentState 屬性。

適用於