共用方式為


Thread.TrySetApartmentState(ApartmentState) 方法

定義

在執行緒啟動之前設定其 Apartment 狀態。

public:
 bool TrySetApartmentState(System::Threading::ApartmentState state);
public bool TrySetApartmentState (System.Threading.ApartmentState state);
member this.TrySetApartmentState : System.Threading.ApartmentState -> bool
Public Function TrySetApartmentState (state As ApartmentState) As Boolean

參數

state
ApartmentState

新的 Apartment 狀態。

傳回

Boolean

如果已設定 Apartment 狀態,則為 true,否則為 false

例外狀況

僅限 .NET Core 和 .NET 5 +:在 macOS 和 Linux 上的所有案例中。

state 的 Apartment 狀態無效。

執行緒曾啟動但已終止,或未在執行緒執行時從執行緒的內容中發出呼叫。

範例

下列程式碼範例示範 GetApartmentStateSetApartmentStateTrySetApartmentState 方法。 程式碼範例會建立一個執行緒。 啟動執行緒之前,會 GetApartmentState 顯示初始 ApartmentState.Unknown 狀態並將 SetApartmentState 狀態變更為 ApartmentState.STATrySetApartmentState當您嘗試將 false 狀態變更為時,方法就會傳回, ApartmentState.MTA 因為已設定了單元狀態。 如果嘗試執行相同的作業,則 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.
 */
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.

備註

新的執行緒會初始化,如同 ApartmentState.MTA 其在啟動之前尚未設定的單元狀態。 在啟動執行緒之前,必須先設定單元狀態。

注意

主應用程式執行緒預設會初始化為 ApartmentState.MTA 。 將主要應用程式執行緒的單元狀態設定為的唯一方法 ApartmentState.STA 是將 STAThreadAttribute 屬性套用至進入點方法。

TrySetApartmentState方法以及 GetApartmentState 方法和 SetApartmentState 方法會取代 ApartmentState 屬性。

適用於