Table of Contents

Class Thread

Namespace
System.Threading
Assembly
mscorlib.dll

Creates and controls a thread, sets its priority, and gets its status.

public sealed class Thread
Inheritance
Thread
Inherited Members
Extension Methods

Constructors

Thread(ThreadStart)

Initializes a new instance of the Thread class.

public Thread(ThreadStart start)

Parameters

start ThreadStart

A ThreadStart delegate that represents the methods to be invoked when this thread begins executing.

Properties

CurrentThread

Gets the currently running thread.

public static Thread CurrentThread { get; }

Property Value

Thread

A Thread that is the representation of the currently running thread.

IsAlive

Gets a value indicating the execution status of the current thread.

public bool IsAlive { get; }

Property Value

bool

true if this thread has been started and has not terminated normally or aborted; otherwise, false.

ManagedThreadId

Gets a unique identifier for the current managed thread.

public int ManagedThreadId { get; }

Property Value

int

An integer that represents a unique identifier for this managed thread.

Priority

Gets or sets a value indicating the scheduling priority of a thread.

public ThreadPriority Priority { get; set; }

Property Value

ThreadPriority

One of the ThreadPriority values. The default value is ThreadPriority.Normal.

ThreadState

Gets a value containing the states of the current thread.

public ThreadState ThreadState { get; }

Property Value

ThreadState

One of the ThreadState values indicating the state of the current thread. The initial value is Unstarted.

Methods

Abort()

Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Calling this method usually terminates the thread.

public void Abort()

Join()

Blocks the calling thread until the thread represented by this instance terminates, while continuing to perform standard COM and SendMessage pumping.

public void Join()

Join(int)

Blocks the calling thread until the thread represented by this instance terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.

public bool Join(int millisecondsTimeout)

Parameters

millisecondsTimeout int

The number of milliseconds to wait for the thread to terminate.

Returns

bool

true if the thread has terminated; false if the thread has not terminated after the amount of time specified by the millisecondsTimeout parameter has elapsed.

Join(TimeSpan)

Blocks the calling thread until the thread represented by this instance terminates or the specified time elapses, while continuing to perform standard COM and SendMessage pumping.

public bool Join(TimeSpan timeout)

Parameters

timeout TimeSpan

A TimeSpan set to the amount of time to wait for the thread to terminate.

Returns

bool

true if the thread terminated; false if the thread has not terminated after the amount of time specified by the timeout parameter has elapsed.

Resume()

Obsolete : Resumes a thread that has been suspended.

public void Resume()

Sleep(int)

Suspends the current thread for the specified number of milliseconds.

public static void Sleep(int millisecondsTimeout)

Parameters

millisecondsTimeout int

The number of milliseconds for which the thread is suspended. If the value of the millisecondsTimeout argument is zero, the thread relinquishes the remainder of its time slice to any thread of equal priority that is ready to run. If there are no other threads of equal priority that are ready to run, execution of the current thread is not suspended.

Remarks

The thread will not be scheduled for execution by the operating system for the amount of time specified. You can specify Timeout.Infinite for the millisecondsTimeout parameter to suspend the thread indefinitely. However, we recommend that you use other System.Threading classes such as AutoResetEvent, ManualResetEvent, Monitor or WaitHandle instead to synchronize threads or manage resources. The system clock ticks at a specific rate called the clock resolution. The actual timeout might not be exactly the specified timeout, because the specified timeout will be adjusted to coincide with clock ticks.

Sleep(TimeSpan)

Suspends the current thread for the specified amount of time.

public static void Sleep(TimeSpan timeout)

Parameters

timeout TimeSpan

The amount of time for which the thread is suspended. If the value of the timeout argument is Zero, the thread relinquishes the remainder of its time slice to any thread of equal priority that is ready to run. If there are no other threads of equal priority that are ready to run, execution of the current thread is not suspended.

Remarks

The thread will not be scheduled for execution by the operating system for the amount of time specified. You can specify Infinite for the timeout parameter to suspend the thread indefinitely. However, we recommend that you use other System.Threading classes such as AutoResetEvent, ManualResetEvent, Monitor or WaitHandle instead to synchronize threads or manage resources. The system clock ticks at a specific rate called the clock resolution. The actual timeout might not be exactly the specified timeout, because the specified timeout will be adjusted to coincide with clock ticks.

SpinWait(int)

Causes a thread to wait the number of times defined by the iterations parameter.

public static void SpinWait(int iterations)

Parameters

iterations int

A 32-bit signed integer that defines how long a thread is to wait.

Remarks

The SpinWait(int) method is useful for implementing locks. Classes in the .NET Framework, such as Monitor use this method internally. SpinWait(int) essentially puts the processor into a very tight loop, with the loop count specified by the iterations parameter. The duration of the wait therefore depends on the speed of the processor. Contrast this with the Sleep(int) method. A thread that calls Sleep(int) yields the rest of its current slice of processor time, even if the specified interval is zero. Specifying a non-zero interval for Sleep(int) removes the thread from consideration by the thread scheduler until the time interval has elapsed.

Start()

Causes the operating system to change the state of the current instance to ThreadState.Running.

public void Start()

Suspend()

Either suspends the thread, or if the thread is already suspended, has no effect.

public void Suspend()