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

其中一個 ThreadState 數值,指出目前執行緒的狀態。 初始值為 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
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

重要

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

適用於