Thread.TrySetApartmentState(ApartmentState) メソッド

定義

スレッドを開始する前にそのアパートメント状態を設定します。Sets the apartment state of a thread before it is started.

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

新しいアパートメント状態。The new apartment state.

戻り値

アパートメント状態が設定されている場合は true。それ以外の場合は falsetrue if the apartment state is set; otherwise, false.

例外

.NET Core のみ: macOS および Linux プラットフォームでは、このメンバーはサポートされていません。.NET Core only: This member is not supported on the macOS and Linux platforms.

state は有効なアパートメント状態ではありません。state is not a valid apartment state.

スレッドが開始され、終了したか、スレッドの実行中にスレッドのコンテキストから呼び出しが行われていません。The thread was started and has terminated, or the call is not being made from the thread's context while the thread is running.

次のコード例は、GetApartmentStateSetApartmentState、および TrySetApartmentState メソッドを示しています。The following code example demonstrates the GetApartmentState, SetApartmentState, and TrySetApartmentState methods. このコード例では、スレッドを作成します。The code example creates a thread. スレッドが開始される前に、GetApartmentState は初期の ApartmentState.Unknown の状態を表示し、SetApartmentState 状態を ApartmentState.STAに変更します。Before the thread is started, GetApartmentState displays the initial ApartmentState.Unknown state and SetApartmentState changes the state to ApartmentState.STA. TrySetApartmentState メソッドは、アパートメント状態が既に設定されているために状態を ApartmentState.MTA に変更しようとしたときに false を返します。The TrySetApartmentState method then returns false when attempting to change the state to ApartmentState.MTA because the apartment state is already set. SetApartmentStateで同じ操作が試行された場合、InvalidOperationException がスローされます。If the same operation had been attempted with SetApartmentState, InvalidOperationException would have been thrown.

スレッドが開始されると、TrySetApartmentState メソッドが再び使用されます。After the thread is started, the TrySetApartmentState method is used again. 今回は、スレッドが既に開始されているため ThreadStateException をスローします。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.

注釈

アパートメント状態が開始前に設定されていない場合、新しいスレッドは ApartmentState.MTA として初期化されます。New threads are initialized as ApartmentState.MTA if their apartment state has not been set before they are started. アパートメント状態は、スレッドを開始する前に設定する必要があります。Apartment state must be set before a thread is started.

注意

メインアプリケーションスレッドは、既定で ApartmentState.MTA に初期化されます。The main application thread is initialized to ApartmentState.MTA by default. メインアプリケーションスレッドのアパートメント状態を ApartmentState.STA に設定する唯一の方法は、STAThreadAttribute 属性をエントリポイントメソッドに適用することです。The only way to set the apartment state of the main application thread to ApartmentState.STA is to apply the STAThreadAttribute attribute to the entry point method.

TrySetApartmentState メソッドと、GetApartmentState メソッドおよび SetApartmentState メソッドを使用して、ApartmentState プロパティが置き換えられます。The TrySetApartmentState method, along with the GetApartmentState method and the SetApartmentState method, replaces the ApartmentState property.

適用対象