ThreadState ThreadState ThreadState ThreadState Enum

定义

指定 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
继承
属性

字段

Aborted Aborted Aborted Aborted 256

线程状态包括 AbortRequested 并且该线程现在已死,但其状态尚未更改为 StoppedThe thread state includes AbortRequested and the thread is now dead, but its state has not yet changed to Stopped.

AbortRequested AbortRequested AbortRequested 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 Background Background 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 Running Running Running 0

线程已启动且尚未停止。The thread has been started and not yet stopped.

Stopped Stopped Stopped Stopped 16

线程已停止。The thread has stopped.

StopRequested StopRequested StopRequested StopRequested 1

正在请求线程停止。The thread is being requested to stop. 这仅用于内部。This is for internal use only.

Suspended Suspended Suspended Suspended 64

线程已挂起。The thread has been suspended.

SuspendRequested SuspendRequested SuspendRequested SuspendRequested 2

正在请求线程挂起。The thread is being requested to suspend.

Unstarted Unstarted Unstarted Unstarted 8

尚未对线程调用 Start() 方法。The Start() method has not been invoked on the thread.

WaitSleepJoin WaitSleepJoin WaitSleepJoin 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.StartA 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. 没有办法知道在哪个点的新线程将开始运行,在调用StartThere 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

适用于

另请参阅