共用方式為


Managed Thread States 

The property Thread.ThreadState provides a bit mask that indicates the thread's current state. A thread is always in at least one of the possible states in the ThreadState enumeration, and can be in multiple states at the same time.

NoteImportant

Thread state is only of interest in a few debugging scenarios. Your code should never use thread state to synchronize the activities of threads.

When you create a managed thread it is in the Unstarted state. The thread remains in the Unstarted state until it is moved into the started state by the operating system. Calling Start lets the operating system know that the thread can be started, it does not change the state of the thread.

Unmanaged threads that enter the managed environment are already in the started state. Once in the started state, there are a number of actions that can cause the thread to change states. The following table lists the actions that cause a change of state, along with the corresponding new state.

Action Resulting new state

Another thread calls System.Threading.Thread.Start.

Unchanged

The thread responds to System.Threading.Thread.Start and starts running.

Running

The thread calls System.Threading.Thread.Sleep.

WaitSleepJoin

The thread calls System.Threading.Monitor.Wait on another object.

WaitSleepJoin

The thread calls System.Threading.Thread.Join on another thread.

WaitSleepJoin

Another thread calls System.Threading.Thread.Suspend.

SuspendRequested

The thread responds to a System.Threading.Thread.Suspend request.

Suspended

Another thread calls System.Threading.Thread.Resume.

Running

Another thread calls System.Threading.Thread.Suspend.

Running

Another thread calls System.Threading.Thread.Abort.

AbortRequested

The thread responds to an System.Threading.Thread.Abort.

Aborted

Because the Running state has a value of 0, it is not possible to perform a bit test to discover this state. Instead, the following test (in pseudo-code) can be used:

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

Threads are often in more than one state at any given time. For example, if a thread is blocked on a System.Threading.Monitor.Wait call and another thread calls Abort on that same thread, the thread will be in both the WaitSleepJoin and the AbortRequested state at the same time. In that case, as soon as the thread returns from the call to Wait or is interrupted, it will receive the ThreadAbortException.

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 never leave the Stopped state.

See Also

Reference

ThreadAbortException
Thread
ThreadState

Other Resources

Managed Threading