ThreadState 枚举

定义

指定 Thread 的执行状态。Specifies the execution states of a Thread.

此枚举有一个 FlagsAttribute 属性,允许按位组合成员值。

public enum class ThreadState
[System.Flags]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public enum ThreadState
type ThreadState = 
Public Enum ThreadState
继承
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) 方法,但线程尚未收到试图终止它的挂起的 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
在公共语言运行时中创建线程。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.
Running
线程调用 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

适用于

另请参阅