托管线程状态

Unstarted state." data-guid="4aae1123257cb7b19eacbd1967a0fc8f">在创建托管线程时,该线程处于 Unstarted 状态。 Unstarted state until it is moved into the started state by the operating system." data-guid="fc8e1e21f2064e2ea2737ad7ace5d444">线程会保持 Unstarted 状态,直到被操作系统转移到已启动状态。 Start lets the operating system know that the thread can be started; it does not change the state of the thread." data-guid="c2dae802f627f50b421f6f7b4d911488">调用 Start 使操作系统知道该线程可启动;它不更改线程的状态。

进入托管环境的非托管线程已处于已启动状态。 线程在启动状态后,许多操作都可使线程更改状态。 下表列出使状态发生更改的操作以及相应的新状态。

操作                                                                                                                                                                  所得到的新状态

 

Thread class is called." data-guid="9fae2d3adec9db969bf62f7ddee3eb1a">调用 Thread 类的构造函数。                     Unstarted

 

另一个线程调用 Thread::Start。                                                                                                                                Unstarted

 

线程响应 Thread::Start 并开始运行。                                                                                                                         Running

 

线程调用 Thread::Sleep

WaitSleepJoin

线程对另一个对象调用 Monitor::Wait

WaitSleepJoin

线程对另一个线程调用 Thread::Join

WaitSleepJoin

另一个线程调用 Thread::Suspend

SuspendRequested

线程响应 Thread::Suspend 请求。

Suspended

另一个线程调用 Thread::Resume

Running

另一个线程调用 Thread::Abort

AbortRequested

线程响应 Thread::Abort

Aborted, thenStopped" data-guid="ff2f3bebdb6b81e23820370820b52269">Aborted ,然后 Stopped

Running state has a value of 0, it is not possible to perform a bit test to discover this state." data-guid="32b990bef154b5a05675743cf1e6259a">由于 Running 状态的值为 0,因此无法执行位测试来发现此状态。 但可以使用以下测试(以伪代码表示)。

if ((state & (Unstarted | Stopped)) == 0)   // implies Running   

在任何给定时间,线程通常处于多个状态中。 例如,如果某个线程在 Monitor::Wait 调用被阻止,并且另一个线程对同一个线程调用 Abort,则该线程将同时处于 WaitSleepJoin  AbortRequested 状态。 Wait or is interrupted, it will receive the ThreadAbortException." data-guid="0c49c67ed6ce4e40757cf01a50db34ad">在这种情况下,一旦该线程从对 Wait 的调用返回或该线程中断,它就会收到 ThreadAbortException

Unstarted state as the result of a call toStart, it can never return to theUnstarted state." data-guid="ffad4edc5491aa637d4300e82117b093">一旦线程由于调用 Start 而离开 Unstarted 状态,那么它将无法再返回到 Unstarted 状态。 Stopped state." data-guid="d322612c5b2794f0fe031129e1e912c1">同样,线程也永远无法离开 Stopped 状态。

你可能感兴趣的:(托管线程状态)