Udostępnij za pośrednictwem


ThreadState Enumeration

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Specifies the execution states of a Thread.

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

Namespace:  System.Threading
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
<FlagsAttribute> _
Public Enumeration ThreadState
[ComVisibleAttribute(true)]
[FlagsAttribute]
public enum ThreadState

Members

Member name Description
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Running The thread has been started, it is not blocked, and there is no pending ThreadAbortException.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 StopRequested The thread is being requested to stop. This is for internal use only.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 SuspendRequested The thread has been marked for suspension.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 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.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Unstarted The Thread.Start method has not been invoked on the thread.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Stopped The thread has stopped.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 WaitSleepJoin The thread is blocked. This could be the result of calling Thread.Sleep or Thread.Join, of requesting a lock — for example, by calling Monitor.Enter or Monitor.Wait — or of waiting on a thread synchronization object such as ManualResetEvent.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Suspended The thread has been suspended.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 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.
Supported by Silverlight for Windows PhoneSupported by Xbox 360 Aborted The thread state includes AbortRequested and the thread is now dead, but its state has not yet changed to Stopped.

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 that are created within the common language runtime are initially in the Unstarted state, whereas external threads that come into the runtime are already in the Running state. An Unstarted thread is transitioned into the Running state by calling the Start method. Not all combinations of ThreadState values are valid; for example, a thread cannot be in both the Aborted and Unstarted states.

Important noteImportant Note:

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

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

Action

ThreadState

A thread is created within the common language runtime.

Unstarted

The thread's Start method is called by another thread.

Unstarted

The thread starts running.

Running

The thread calls Sleep.

WaitSleepJoin

The thread calls Wait or makes some other blocking call.

WaitSleepJoin

The thread calls Join on another thread.

WaitSleepJoin

The common language runtime marks the thread for suspension, for example, in order to do garbage collection.

SuspendRequested

The thread is suspended.

Suspended

The thread resumes operation after being suspended.

Running

Another thread calls Thread.Abort on the thread.

AbortRequested

The thread is terminated.

Stopped

The Thread.IsBackground property of the thread is set to true either by the thread itself or by another thread.

Background

A thread can be in more than one state at a given time. For example:

  • The Background state can be combined with any other state.

  • If a thread is blocked on a call to Monitor.Wait and the system aborts it, 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 and 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. Because the value for Running is 0 (zero), 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.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

See Also

Reference

Other Resources