Class Thread
Creates and controls a thread, sets its priority, and gets its status.
Inherited Members
Namespace: System.Threading
Assembly: mscorlib.dll
Syntax
public sealed class Thread
Constructors
Thread(ThreadStart)
Initializes a new instance of the Thread class.
Declaration
public extern Thread(ThreadStart start)
Parameters
Type | Name | Description |
---|---|---|
ThreadStart | start | A ThreadStart delegate that represents the methods to be invoked when this thread begins executing. |
Properties
CurrentThread
Gets the currently running thread.
Declaration
public static Thread CurrentThread { get; }
Property Value
Type | Description |
---|---|
Thread | A Thread that is the representation of the currently running thread. |
IsAlive
Gets a value indicating the execution status of the current thread.
Declaration
public bool IsAlive { get; }
Property Value
Type | Description |
---|---|
Boolean | 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.
Declaration
public int ManagedThreadId { get; }
Property Value
Type | Description |
---|---|
Int32 | An integer that represents a unique identifier for this managed thread. |
Priority
Gets or sets a value indicating the scheduling priority of a thread.
Declaration
public ThreadPriority Priority { get; set; }
Property Value
Type | Description |
---|---|
ThreadPriority | One of the ThreadPriority values. The default value is ThreadPriority.Normal. |
ThreadState
Gets a value containing the states of the current thread.
Declaration
public ThreadState ThreadState { get; }
Property Value
Type | Description |
---|---|
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.
Declaration
public extern void Abort()
Join()
Blocks the calling thread until the thread represented by this instance terminates, while continuing to perform standard COM and SendMessage pumping.
Declaration
public extern void Join()
Join(Int32)
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.
Declaration
public extern bool Join(int millisecondsTimeout)
Parameters
Type | Name | Description |
---|---|---|
Int32 | millisecondsTimeout | The number of milliseconds to wait for the thread to terminate. |
Returns
Type | Description |
---|---|
Boolean | 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.
Declaration
public extern bool Join(TimeSpan timeout)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | timeout | A TimeSpan set to the amount of time to wait for the thread to terminate. |
Returns
Type | Description |
---|---|
Boolean | 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.
Declaration
public extern void Resume()
Sleep(Int32)
Suspends the current thread for the specified number of milliseconds.
Declaration
public static extern void Sleep(int millisecondsTimeout)
Parameters
Type | Name | Description |
---|---|---|
Int32 | millisecondsTimeout | The number of milliseconds for which the thread is suspended.
If the value of the |
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.
Declaration
public static void Sleep(TimeSpan timeout)
Parameters
Type | Name | Description |
---|---|---|
TimeSpan | timeout | The amount of time for which the thread is suspended.
If the value of the |
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(Int32)
Causes a thread to wait the number of times defined by the iterations
parameter.
Declaration
public static extern void SpinWait(int iterations)
Parameters
Type | Name | Description |
---|---|---|
Int32 | iterations | A 32-bit signed integer that defines how long a thread is to wait. |
Remarks
The SpinWait(Int32) method is useful for implementing locks. Classes in the .NET Framework, such as Monitor use this method internally. SpinWait(Int32) 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(Int32) method. A thread that calls Sleep(Int32) yields the rest of its current slice of processor time, even if the specified interval is zero. Specifying a non-zero interval for Sleep(Int32) 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.
Declaration
public extern void Start()
Suspend()
Either suspends the thread, or if the thread is already suspended, has no effect.
Declaration
public extern void Suspend()