次の方法で共有


Thread.GetApartmentState メソッド

定義

アパートメント状態を示す ApartmentState 値を返します。

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

戻り値

マネージド スレッドのアパートメント状態を示す ApartmentState 値の 1 つ。 既定値は、Unknown です。

次のコード例は、GetApartmentStateSetApartmentStateおよび TrySetApartmentState の各メソッドを示しています。 このコード例では、スレッドを作成します。 スレッドが開始される前に、 GetApartmentState 初期状態 ApartmentState.Unknown を表示し、 SetApartmentState 状態を に ApartmentState.STA変更します。 その後、アパートメント状態がTrySetApartmentState既に設定されているため、状態を にApartmentState.MTA変更しようとすると、 メソッドが を返falseします。 で同じ操作が試行されたSetApartmentStateInvalidOperationException場合は、 がスローされました。

スレッドが開始されると、 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.

注釈

このメソッドは、 メソッドと メソッドと共 SetApartmentStateTrySetApartmentState 、 プロパティを ApartmentState 置き換えます。

適用対象