ThreadState 列舉


指定 Thread 的執行狀態。Specifies the execution states of a Thread.

此列舉具有 FlagsAttribute 個屬性允許以位元方式合併其成員值。

public enum class ThreadState
public enum ThreadState
public enum ThreadState
public enum ThreadState
public enum ThreadState
public enum ThreadState
type ThreadState = 
Public Enum ThreadState


Aborted 256

執行緒狀態包括 AbortRequested,且執行緒目前無作用,但其狀態尚未變更為 StoppedThe thread state includes AbortRequested and the thread is now dead, but its state has not yet changed to Stopped.

AbortRequested 128

Abort(Object) 方法已在執行緒上被叫用 (Invoke),但執行緒還沒有收到會嘗試終結它的暫止 ThreadAbortExceptionThe Abort(Object) method has been invoked on the thread, but the thread has not yet received the pending ThreadAbortException that will attempt to terminate it.

Background 4

執行緒正做為背景執行緒執行當中 (相對於前景執行緒)。The thread is being executed as a background thread, as opposed to a foreground thread. 這個狀態以設定 IsBackground 屬性來控制。This state is controlled by setting the IsBackground property.

Running 0

執行緒已啟動且尚未停止。The thread has been started and not yet stopped.

Stopped 16

執行緒已經停止。The thread has stopped.

StopRequested 1

執行緒正被要求停止中。The thread is being requested to stop. 僅供內部使用。This is for internal use only.

Suspended 64

執行緒已經暫止。The thread has been suspended.

SuspendRequested 2

執行緒正被要求暫止中。The thread is being requested to suspend.

Unstarted 8

Start() 方法還沒有在執行緒上被叫用。The Start() method has not been invoked on the thread.

WaitSleepJoin 32

執行緒已封鎖。The thread is blocked. 這可能是呼叫 Sleep(Int32)Join()、要求鎖定 (例如藉由呼叫 Enter(Object)Wait(Object, Int32, Boolean)),或是在執行緒同步處理物件 (例如 ManualResetEvent) 上等候的結果。This could be the result of calling Sleep(Int32) or Join(), of requesting a lock - for example, by calling Enter(Object) or Wait(Object, Int32, Boolean) - or of waiting on a thread synchronization object such as ManualResetEvent.


ThreadState 列舉會為執行緒定義一組所有可能的執行狀態。The ThreadState enumeration defines a set of all possible execution states for threads. 只有幾個偵錯工具才會對其感興趣。It's of interest only in a few debugging scenarios. 您的程式碼絕對不應使用執行緒狀態來同步處理執行緒的活動。Your code should never use the thread state to synchronize the activities of threads.

一旦建立執行緒之後,它就會處於至少其中一個狀態,直到終止為止。Once a thread is created, it's in at least one of the states until it terminates. 在通用語言執行平臺內建立的執行緒一開始會處於 Unstarted 狀態,而外部或非受控時,進入執行時間的執行緒已經處於 Running 狀態。Threads created within the common language runtime are initially in the Unstarted state, while external, or unmanaged, threads that come into the runtime are already in the Running state. 執行緒會藉由呼叫 Thread.Start,從 Unstarted 狀態轉換為 Running 狀態。A thread is transitioned from the Unstarted state into the Running state by calling Thread.Start. 一旦執行緒因為呼叫 Unstarted 而離開 Start狀態,它便無法再回到 Unstarted 狀態。Once a thread leaves the Unstarted state as the result of a call to Start, it can never return to the Unstarted state.

一個執行緒可以在指定的時間內處於一個以上的狀態。A thread can be in more than one state at a given time. 例如,如果在對 Monitor.Wait的呼叫上封鎖執行緒,而且另一個執行緒在封鎖的執行緒上呼叫 Thread.Abort,則封鎖的執行緒會同時處於 WaitSleepJoinAbortRequested 狀態。For example, if a thread is blocked on a call to Monitor.Wait, and another thread calls Thread.Abort on the blocked thread, the blocked thread will be in both the WaitSleepJoin and AbortRequested states at the same time. 在此情況下,只要執行緒從呼叫傳回 Monitor.Wait 或中斷,就會收到 ThreadAbortException 開始中止。In this case, as soon as the thread returns from the call to Monitor.Wait or is interrupted, it will receive the ThreadAbortException to begin aborting. 並非所有 ThreadState 值的組合都有效;例如,執行緒不能同時處於 AbortedUnstarted 狀態。Not all combinations of ThreadState values are valid; for example, a thread cannot be in both the Aborted and Unstarted states.

執行緒永遠無法離開 Stopped 狀態。A thread can never leave the Stopped state.


有兩個執行緒狀態列舉: System.Threading.ThreadStateSystem.Diagnostics.ThreadStateThere are two thread state enumerations: System.Threading.ThreadState and System.Diagnostics.ThreadState.

下表顯示導致狀態變更的動作。The following table shows the actions that cause a change of state.

動作Action ThreadStateThreadState
在 common language runtime 內建立執行緒。A thread is created within the common language runtime. Unstarted
另一個執行緒會在新的執行緒上呼叫 Thread.Start 方法,而呼叫會傳回。Another thread calls the Thread.Start method on the new thread, and the call returns.

在新執行緒開始執行之前,Start 方法不會傳回。The Start method does not return until the new thread has started running. Start的呼叫期間,無法得知新執行緒開始執行的時間點。There is no way to know at what point the new thread will start running, during the call to Start.
執行緒會呼叫 SleepThe thread calls Sleep WaitSleepJoin
執行緒對另一個物件呼叫 Monitor.WaitThe thread calls Monitor.Wait on another object. WaitSleepJoin
執行緒對另一個執行緒呼叫 JoinThe thread calls Join on another thread. WaitSleepJoin
另一個執行緒呼叫 InterruptAnother thread calls Interrupt Running
另一個執行緒呼叫 SuspendAnother thread calls Suspend SuspendRequested
執行緒回應 Suspend 要求。The thread responds to a Suspend request. Suspended
另一個執行緒呼叫 ResumeAnother thread calls Resume Running
另一個執行緒呼叫 AbortAnother thread calls Abort AbortRequested
執行緒會回應 Abort 的要求。The thread responds to an Abort request. Stopped
執行緒已終止。A thread is terminated. Stopped

除了上面所述的狀態以外,還有 Background 狀態,指出執行緒是在背景還是前景中執行。In addition to the states noted above, there is also the Background state, which indicates whether the thread is running in the background or foreground. 如需詳細資訊,請參閱前景與背景執行緒For more information, see Foreground and Background Threads.

執行緒的 Thread.ThreadState 屬性會提供執行緒的目前狀態。The Thread.ThreadState property of a thread provides the current state of a thread. 應用程式必須使用位元遮罩來判斷線程是否正在執行。Applications must use a bit mask to determine whether a thread is running. 由於 Running 的值為零(0),請測試執行緒是否正由下列程式碼執行:Since the value for Running is zero (0), test whether a thread is running by the following code:

(myThread.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted)) == 0
(myThread.ThreadState And (ThreadState.Stopped Or ThreadState.Unstarted)) = 0