Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.
0 out of 1 rated this helpful - Rate this topic

ThreadState Enumeration

Specifies the execution states of a Thread.

This enumeration has a FlagsAttribute attribute that allows a bitwise combination of its member values.

[Visual Basic]
<Flags>
<Serializable>
Public Enum ThreadState
[C#]
[Flags]
[Serializable]
public enum ThreadState
[C++]
[Flags]
[Serializable]
__value public enum ThreadState
[JScript]
public
   Flags
 Serializable
enum ThreadState

Remarks

ThreadState defines a set of all possible execution states for threads. Once a thread is created, it is in at least one of the states until it terminates. Threads created within the common language runtime are initially in the Unstarted state, while external threads that come into the runtime are already in the Running state. An Unstarted thread is transitioned into the Running state by calling Start. Not all combinations of ThreadState values are valid; for example, a thread cannot be in both the Aborted and Unstarted states.

The following table shows the actions that cause a change of state.

Action ThreadState
A thread is created within the common language runtime. Unstarted
A thread calls Start Running
The thread starts running. Running
The thread calls Sleep WaitSleepJoin
The thread calls Wait on another object. WaitSleepJoin
The thread calls Join on another thread. WaitSleepJoin
Another thread calls Interrupt Running
Another thread calls Suspend SuspendRequested
The thread responds to a Suspend request. Suspended
Another thread calls Resume Running
Another thread calls Abort AbortRequested
The thread responds to a Abort request. Stopped
A thread is terminated. Stopped

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.

A thread can be in more than one state at a given time. For example, if a thread is blocked on a call to Wait, and another thread calls Abort on the blocked thread, the blocked thread will be in both the WaitSleepJoin and the AbortRequested states at the same time. In this case, as soon as the thread returns from the call to Wait or is interrupted, it will receive the ThreadAbortException to begin aborting.

The Thread.ThreadState property of a thread provides the current state of a thread. Applications must use a bitmask to determine whether a thread is running. Since the value for Running is zero (0), test whether a thread is running by using C# code such as (myThread.ThreadState & (ThreadState.Stopped | ThreadState.Unstarted)) == 0 or Visual Basic code such as (myThread.ThreadState And (ThreadState.Stopped Or ThreadState.Unstarted)) = 0.

Members

Member name Description Value
Aborted The thread is in the Stopped state. 256
AbortRequested The Thread.Abort method has been invoked on the thread, but the thread has not yet received the pending System.Threading.ThreadAbortException that will attempt to terminate it. 128
Background The thread is being executed as a background thread, as opposed to a foreground thread. This state is controlled by setting the Thread.IsBackground property. 4
Running The thread has been started, it is not blocked, and there is no pending ThreadAbortException. 0
Stopped The thread has stopped. 16
StopRequested The thread is being requested to stop. This is for internal use only. 1
Suspended The thread has been suspended. 64
SuspendRequested The thread is being requested to suspend. 2
Unstarted The Thread.Start method has not been invoked on the thread. 8
WaitSleepJoin The thread is blocked as a result of a call to Wait, Sleep, or Join. 32

Requirements

Namespace: System.Threading

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

System.Threading Namespace | Thread | Thread States | Managed and Unmanaged Threading

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.