Thread.ThreadState 屬性

定義

取得數值,包含目前執行緒的狀態。

C#
public System.Threading.ThreadState ThreadState { get; }

屬性值

ThreadState

其中一個 ThreadState 數值,指出目前執行緒的狀態。 初始值為 Unstarted

範例

下列程式碼範例將示範如何存取 ThreadState 執行緒的。

C#
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

備註

ThreadState屬性會提供比屬性更明確的資訊 IsAlive

重要

執行緒狀態只有在偵錯工具案例中才有意義。 您的程式碼絕對不應該使用執行緒狀態來同步處理執行緒活動。

適用於

產品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard 2.0, 2.1