線程對象使用ThreadState.屬性指示線程狀態。
ThreadState是帶flag特性的枚舉類型對象,因此判斷線程當前的狀態必須用bitmask,作為一個特例,由於Running狀態的bit碼是0,因此,需要用如下方式判斷線程是否處於運行狀態:(myThread.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted)) == 0。
枚舉成員如下:
|
Member name |
Description |
Running
The thread has been started, it is not blocked, and there is no pendingThreadAbortException.
StopRequested
The thread is being requested to stop. This is for internal use only.
SuspendRequested
The thread is being requested to suspend.
Background
The thread is being executed as a background thread, as opposed to a foreground thread. This state is controlled by setting theThread.IsBackground property.
Unstarted
The Thread.Start method has not been invoked on the thread.
Stopped
The thread has stopped.
WaitSleepJoin
The thread is blocked. This could be the result of calling Thread.Sleep orThread.Join, of requesting a lock — for example, by callingMonitor.Enter orMonitor.Wait — or of waiting on a thread synchronization object such asManualResetEvent.
Suspended
The thread has been suspended.
AbortRequested
The Thread.Abort method has been invoked on the thread, but the thread has not yet received the pendingSystem.Threading.ThreadAbortException that will attempt to terminate it.
Aborted
The thread state includes AbortRequested and the thread is now dead, but its state has not yet changed toStopped.
狀態轉換圖如下“