Thread.SetApartmentState(ApartmentState) Thread.SetApartmentState(ApartmentState) Thread.SetApartmentState(ApartmentState) Thread.SetApartmentState(ApartmentState) Method

定義

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

public:
 void SetApartmentState(System::Threading::ApartmentState state);
public void SetApartmentState (System.Threading.ApartmentState state);
member this.SetApartmentState : System.Threading.ApartmentState -> unit
Public Sub SetApartmentState (state As ApartmentState)

パラメーター

state
ApartmentState ApartmentState ApartmentState ApartmentState

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

例外

.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 has already been started.

アパートメント状態は既に初期化されています。The apartment state has already been initialized.

次のコード例に示します、 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メソッドが戻りますfalseに状態を変更しようとしています。ApartmentState.MTAアパートメント状態が既に設定されているためです。The TrySetApartmentState method then returns false when attempting to change the state to ApartmentState.MTA because the apartment state is already set. 場合は、同じ操作でSetApartmentStateInvalidOperationExceptionがスローされました。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
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.

SetApartmentStateメソッドと共に、GetApartmentStateメソッドとTrySetApartmentStateメソッド、置換、ApartmentStateプロパティ。The SetApartmentState method, along with the GetApartmentState method and the TrySetApartmentState method, replaces the ApartmentState property.

適用対象