public sealed class Thread
|
For the duration of its existence, a thread is always in one or more of the states defined by ThreadState. A scheduling priority level, as defined by ThreadPriority, can be requested for a thread, but is not guaranteed to be honored by the operating system.
CurrentContext | Read-only Gets the current context in which the thread is executing. |
CurrentCulture | Read-write Gets or sets the culture for the current thread. |
CurrentPrincipal | Read-write Gets or sets the thread's current principal (for role-based security). |
CurrentThread | Read-only Gets the currently running thread. |
CurrentUICulture | Read-write Gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run time. |
IsAlive | Read-only Gets a value indicating the execution status of the current thread. |
IsBackground | Read-write Gets or sets a value indicating whether or not a thread is a background thread. |
IsThreadPoolThread | Read-only |
Name | Read-write Gets or sets the name of the thread. |
Priority | Read-write Gets or sets a value indicating the scheduling priority of a thread. |
ThreadState | Read-only Gets a value containing the states of the current thread. |
Abort | Overloaded: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. |
Abort | Overloaded:Abort(object stateInfo) Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread while also providing exception information about the thread termination. Calling this method usually terminates the thread. |
AllocateDataSlot | Allocates an unnamed data slot on all the threads. |
AllocateNamedDataSlot | Allocates a named data slot on all threads. |
Equals (inherited from System.Object) |
See base class member description: System.Object.Equals Derived from System.Object, the primary base class for all objects. |
FreeNamedDataSlot | Frees a previously allocated named data slot. |
GetData | Retrieves the value from the specified slot on the current thread, within the current thread's current domain. |
GetDomain | Returns the current domain in which the current thread is running. |
GetDomainID | Returns a unique application domain identifier. |
GetHashCode (inherited from System.Object) |
See base class member description: System.Object.GetHashCode Derived from System.Object, the primary base class for all objects. |
GetNamedDataSlot | Looks up a named data slot. |
GetType (inherited from System.Object) |
See base class member description: System.Object.GetType Derived from System.Object, the primary base class for all objects. |
Interrupt | Interrupts a thread that is in the WaitSleepJoin thread state. |
Join | Overloaded:Join() Blocks the calling thread until a thread terminates. |
Join | Overloaded:Join(int millisecondsTimeout) Blocks the calling thread until a thread terminates or the specified time elapses. |
Join | Overloaded:Join(TimeSpan timeout) Blocks the calling thread until a thread terminates or the specified time elapses. |
ResetAbort | Cancels an Thread.Abort requested for the current thread. |
Resume | Resumes a thread that has been suspended. |
SetData | Sets the data in the specified slot on the currently running thread, for that thread's current domain. |
Sleep | Overloaded:Sleep(int millisecondsTimeout) Suspends the current thread for a specified time. |
Sleep | Overloaded:Sleep(TimeSpan timeout) Blocks the current thread for a specified time. |
SpinWait | Causes a thread to wait the number of times defined by the iterations parameter. |
Start | Causes the operating system to change the state of the current instance to ThreadState.Running. |
Suspend | Either suspends the thread, or if the thread is already suspended, has no effect. |
ToString (inherited from System.Object) |
See base class member description: System.Object.ToString Derived from System.Object, the primary base class for all objects. |
Finalize | Overridden: Releases the resources held by this instance. |
MemberwiseClone (inherited from System.Object) |
See base class member description: System.Object.MemberwiseClone Derived from System.Object, the primary base class for all objects. |
Hierarchy:
public Thread(Thread( |
start
Exception Type | Condition |
---|---|
ArgumentNullException | The start parameter is null. |
public static Context CurrentContext {get;}
|
public CultureInfo CurrentCulture {get; set;}
|
public static IPrincipal CurrentPrincipal {get; set;}
|
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission. |
public static Thread CurrentThread {get;}
|
public CultureInfo CurrentUICulture {get; set;}
|
Exception Type | Condition |
---|---|
ArgumentNullException | The property value is null. |
public bool IsAlive {get;}
|
public bool IsBackground {get; set;}
|
Exception Type | Condition |
---|---|
ThreadStateException | The thread has not been started, is dead, or is not in the suspended state. |
public bool IsThreadPoolThread {get;}
|
public string Name {get; set;}
|
Exception Type | Condition |
---|---|
InvalidOperationException | A set operation was requested, and the Name property has already been set. |
public ThreadPriority Priority {get; set;}
|
Exception Type | Condition |
---|---|
ThreadStateException | The thread has reached a final state, such as ThreadState.Aborted. |
ArgumentException | The value specified for a set operation is not a valid ThreadPriority value. |
Operating systems are not required to honor the priority of a thread.
public ThreadState ThreadState {get;}
|
public void Abort(); |
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission for this thread. |
The thread is not guaranteed to abort immediately, or at all. This situation can occur if a thread does an unbounded amount of computation in the finally blocks that are called as part of the abort procedure, thereby indefinitely delaying the abort. To ensure a thread has aborted, invoke a Thread.Join method on the thread after calling Abort.
If Abort is called on a thread that has not been started, the thread will abort when Thread.Start is called. If Abort is called on a thread that has been suspended, the thread is resumed and then aborted. If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted. If, while executing unmanaged code, a thread ignores a ThreadAbortException, the system re-throws the ThreadAbortException when the thread begins executing managed code.
If two calls to Abort come at the same time, it is possible that one call set the state information and the other call executes the Abort. However, an application cannot distinguish this.
After Abort is invoked on a thread, the state of the thread includes ThreadState.AbortRequested. After the thread has terminated as a result of a successful call to Abort, the state of the thread is changed to ThreadState.Stopped. With sufficient permissions, a thread that is the target of an Abort can cancel the abort using the ResetAbort method. For an example that demonstrates calling the ResetAbort method, see the ThreadAbortException class.
public void Abort( |
stateInfo
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the required permission for this thread. |
The thread is not guaranteed to abort immediately, or at all. This situation can occur if a thread does an unbounded amount of computation in the finally blocks that are called as part of the abort procedure, thereby indefinitely delaying the abort. To ensure a thread has aborted, invoke a Thread.Join method on the thread after calling Abort.
If Abort is called on a thread that has not been started, the thread will abort when Thread.Start is called. If Abort is called on a thread that has been suspended, the thread is resumed and then aborted. If Abort is called on a thread that is blocked or is sleeping, the thread is interrupted and then aborted. If, while executing unmanaged code, a thread ignores a ThreadAbortException, the system re-throws the ThreadAbortException when the thread begins executing managed code.
If two calls to Abort come at the same time, it is possible that one call sets the state information, and the other call executes the Abort. However, an application cannot distinguish this.
After Abort is invoked on a thread, the state of the thread includes ThreadState.AbortRequested. After the thread has terminated as a result of a successful call to Abort, the state of the thread is changed to ThreadState.Stopped. With sufficient permissions, a thread that is the target of an Abort can cancel the abort using the ResetAbort method. For an example that demonstrates calling the ResetAbort method, see the ThreadAbortException class.
public static LocalDataStoreSlot AllocateDataSlot(); |
Threads use a local store memory mechanism to store thread-specific data. The common language runtime allocates a multi-slot data store array to each process when it is created. The thread can allocate a data slot in the data store, store and retrieve a data value in the slot, and free the slot for reuse after the thread expires. Data slots are unique per thread. No other thread (not even a child thread) can get that data.
public static LocalDataStoreSlot AllocateNamedDataSlot( |
name
Slots allocated with this method must be freed with Thread.FreeNamedDataSlot.
~Thread(); |
See the conceptual topic at MSDN: programmingessentialsforgarbagecollection for more information.
public static void FreeNamedDataSlot( |
name
Threads use a local store memory mechanism to store thread-specific data. The common language runtime allocates a multi-slot data store array to each process when it is created. The thread can allocate a data slot in the data store, store and retrieve a data value in the slot, and free the slot for reuse after the thread expires. Data slots are unique per thread. No other thread (not even a child thread) can get that data.
public static object GetData( |
slot
public static AppDomain GetDomain(); |
public static int GetDomainID(); |
public virtual int GetHashCode(); |
public static LocalDataStoreSlot GetNamedDataSlot( |
name
If the named slot does not exist, a new slot is allocated. Named data slots are public and can be manipulated by anyone.
public Type GetType(); |
public void Interrupt(); |
Exception Type | Condition |
---|---|
SecurityException | The caller does not have the appropriate SecurityPermission. |
public void Join(); |
Exception Type | Condition |
---|---|
ThreadStateException | The caller attempted to join a thread that is in the ThreadState.Unstarted state. |
ThreadInterruptedException | The thread is interrupted while waiting. |
This method changes the state of the calling thread to include ThreadState.WaitSleepJoin. You cannot invoke Join on a thread that is in the ThreadState.Unstarted state.
millisecondsTimeout
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The value of millisecondsTimeout is negative and is not equal to Timeout.Infinite in milliseconds. |
ThreadStateException | The thread has not been started. |
This method changes the state of the calling thread to include ThreadState.WaitSleepJoin. You cannot invoke Join on a thread that is in the ThreadState.Unstarted state.
timeout
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The value of timeout is negative and is not equal to Timeout.Infinite in milliseconds, or is greater than Int32.MaxValue milliseconds. |
ThreadStateException | The caller attempted to join a thread that is in the ThreadState.Unstarted state. |
This method changes the state of the current thread to include ThreadState.WaitSleepJoin. You cannot invoke Join on a thread that is in the ThreadState.Unstarted state.
protected object MemberwiseClone(); |
public static void ResetAbort(); |
Exception Type | Condition |
---|---|
ThreadStateException | Abort was not invoked on the current thread. |
SecurityException | The caller does not have the required security permission for the current thread. |
When a call is made to Abort to terminate a thread, the system throws a ThreadAbortException.ThreadAbortException is a special exception that can be caught by application code, but is rethrown at the end of the catch block unless ResetAbort is called.ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread.
See ThreadAbortException for an example that demonstrates calling the ResetAbort method.
public void Resume(); |
Exception Type | Condition |
---|---|
ThreadStateException | The thread has not been started, is dead, or is not in the suspended state. |
SecurityException | The caller does not have the appropriate SecurityPermission. |
public static void SetData( |
slot
data
public static void Sleep( |
millisecondsTimeout
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The timeout value is negative and is not equal to Timeout.Infinite. |
public static void Sleep( |
timeout
Exception Type | Condition |
---|---|
ArgumentOutOfRangeException | The value of timeout is negative and is not equal to Timeout.Infinite in milliseconds, or is greater than Int32.MaxValue milliseconds. |
public static void SpinWait( |
iterations
public void Start(); |
Exception Type | Condition |
---|---|
ThreadStateException | The thread has already been started. |
SecurityException | The caller does not have the appropriate SecurityPermission. |
OutOfMemoryException | There is not enough memory available to start this thread. |
NullReferenceException | This method was invoked on a thread reference that is null. |
Once the thread terminates, it cannot be restarted with another call to Start.
This code produces the following output:
using System; using System.Threading; public class ThreadWork { public static void DoWork() { for(int i = 0; i<3;i++) { Console.WriteLine("Working thread..."); Thread.Sleep(100); } } } class ThreadTest { public static void Main() { ThreadStart myThreadDelegate = new ThreadStart(ThreadWork.DoWork); Thread myThread = new Thread(myThreadDelegate); myThread.Start(); for(int i = 0; i<3; i++) { Console.WriteLine("In main."); Thread.Sleep(100); } } }
In main. Working thread... In main. Working thread... In main. Working thread...
Note that the sequence of the output statements is typical, but is not guaranteed to be identical across systems.
public void Suspend(); |
Exception Type | Condition |
---|---|
ThreadStateException | The thread has not been started or is dead. |
SecurityException | The caller does not have the appropriate SecurityPermission. |
public virtual string ToString(); |