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. 线程通过调用Unstarted Running 从状态Thread.Start转换到状态。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 , 则阻塞的线程WaitSleepJoin将同时处于和AbortRequested状态。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返回或中断, 它就会接收到开始终止的。 ThreadAbortExceptionIn 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组合都有效; 例如, 一个线程不能Aborted同时处于和Unstarted状态。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.

直到新线程开始运行,方法才会返回。StartThe 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

适用于

另请参阅