Thread.ThreadState プロパティ

定義

現在のスレッドの状態を示す値を取得します。

public:
 property System::Threading::ThreadState ThreadState { System::Threading::ThreadState get(); };
public System.Threading.ThreadState ThreadState { get; }
member this.ThreadState : System.Threading.ThreadState
Public ReadOnly Property ThreadState As ThreadState

プロパティ値

現在のスレッドの状態を示す ThreadState 値の 1 つ。 初期値は Unstarted です。

次のコード例では、スレッドの に ThreadState アクセスする方法を示します。

using namespace System;
using namespace System::Threading;

// ref class ApartmentTest
// {
// public:
   static void ThreadMethod()
   {
      Thread::Sleep( 1000 );
//    }

};

int main()
{
//    Thread^ newThread = gcnew Thread( gcnew ThreadStart( &ApartmentTest::ThreadMethod ) );
   Thread^ newThread = gcnew Thread( gcnew ThreadStart( &ThreadMethod ) );

   Console::WriteLine("ThreadState: {0}", newThread->ThreadState);
   newThread->Start();
   
   // Wait for newThread to start and go to sleep.
   Thread::Sleep(300);
   Console::WriteLine("ThreadState: {0}", newThread->ThreadState);

   // Wait for newThread to restart.
   Thread::Sleep(1000);
   Console::WriteLine("ThreadState: {0}", newThread->ThreadState);
}
// The example displays the following output:
//       ThreadState: Unstarted
//       ThreadState: WaitSleepJoin
//       ThreadState: Stopped
using System;
using System.Threading;

class Example
{
    static void Main()
    {
        Thread newThread = 
            new Thread(new ThreadStart(ThreadMethod));

        Console.WriteLine("ThreadState: {0}", newThread.ThreadState);
        newThread.Start();

        // Wait for newThread to start and go to sleep.
        Thread.Sleep(300);
        Console.WriteLine("ThreadState: {0}", newThread.ThreadState);
        
        // Wait for newThread to restart.
        Thread.Sleep(1000);
        Console.WriteLine("ThreadState: {0}", newThread.ThreadState);
    }

    static void ThreadMethod()
    {
        Thread.Sleep(1000);
    }
}
// The example displays the following output:
//       ThreadState: Unstarted
//       ThreadState: WaitSleepJoin
//       ThreadState: Stopped
open System.Threading

let threadMethod () =
    Thread.Sleep 1000

let newThread = Thread threadMethod

printfn $"ThreadState: {newThread.ThreadState}"
newThread.Start()

// Wait for newThread to start and go to sleep.
Thread.Sleep 300
printfn $"ThreadState: {newThread.ThreadState}"

// Wait for newThread to restart.
Thread.Sleep 1000
printfn $"ThreadState: {newThread.ThreadState}"

// The example displays the following output:
//       ThreadState: Unstarted
//       ThreadState: WaitSleepJoin
//       ThreadState: Stopped
Imports System.Threading

Public Module Example
    Public Sub Main()
        Dim newThread As Thread = New Thread(AddressOf ThreadMethod)

        Console.WriteLine("ThreadState: {0}", newThread.ThreadState)
        newThread.Start()

        ' Wait for newThread to start and go to sleep.
        Thread.Sleep(300)
        Console.WriteLine("ThreadState: {0}", newThread.ThreadState)

        ' Wait for newThread to restart.
        Thread.Sleep(1000)
        Console.WriteLine("ThreadState: {0}", newThread.ThreadState)
    End Sub

    Sub ThreadMethod()
        Thread.Sleep(1000)
    End Sub
End Module
' The example displays the following output:
'       ThreadState: Unstarted
'       ThreadState: WaitSleepJoin
'       ThreadState: Stopped

注釈

プロパティは ThreadState 、 プロパティよりも具体的な情報を IsAlive 提供します。

重要

スレッドの状態は、デバッグ シナリオでのみ重要です。 スレッドの動作を同期化する目的でコード内でスレッドの状態を使用しないでください。

適用対象