Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Methods of the Thread Class
The Thread class in C# provides several methods to control and manage thread execution. The most commonly used methods are Start(), Sleep(), Join(), and Abort() (though Abort() is obsolete in .NET Core and later versions).
Commonly Used Thread Methods
| Method | Description |
|---|---|
| Start() | Starts the execution of a thread |
| Sleep(int milliseconds) | Pauses the current thread for specified time |
| Join() | Blocks calling thread until this thread terminates |
| Interrupt() | Interrupts a thread in WaitSleepJoin state |
Using Start() and Join() Methods
The Start() method begins thread execution, while Join() makes the calling thread wait until the target thread completes −
using System;
using System.Threading;
class Program {
static void WorkerMethod() {
for (int i = 1; i <= 3; i++) {
Console.WriteLine("Worker thread: " + i);
Thread.Sleep(1000);
}
Console.WriteLine("Worker thread completed");
}
public static void Main() {
Thread workerThread = new Thread(WorkerMethod);
Console.WriteLine("Starting worker thread...");
workerThread.Start();
Console.WriteLine("Main thread continues...");
workerThread.Join(); // Wait for worker thread to finish
Console.WriteLine("Main thread completed");
}
}
The output of the above code is −
Starting worker thread... Main thread continues... Worker thread: 1 Worker thread: 2 Worker thread: 3 Worker thread completed Main thread completed
Using Sleep() Method
The Sleep() method pauses the current thread for a specified duration −
using System;
using System.Threading;
class Program {
public static void Main() {
Console.WriteLine("Thread starts at: " + DateTime.Now.ToString("HH:mm:ss"));
Thread.Sleep(2000); // Sleep for 2 seconds
Console.WriteLine("Thread resumes at: " + DateTime.Now.ToString("HH:mm:ss"));
Console.WriteLine("Thread slept for 2 seconds");
}
}
The output of the above code is −
Thread starts at: 14:23:45 Thread resumes at: 14:23:47 Thread slept for 2 seconds
Complete List of Thread Methods
| Sr.No. | Method & Description |
|---|---|
| 1 |
public void Abort() Raises a ThreadAbortException in the thread on which it is invoked, to begin the process of terminating the thread. Note: Obsolete in .NET Core and later versions. |
| 2 |
public static LocalDataStoreSlot AllocateDataSlot() Allocates an unnamed data slot on all the threads. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead. |
| 3 |
public static LocalDataStoreSlot AllocateNamedDataSlot(string name) Allocates a named data slot on all threads. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead. |
| 4 |
public static void BeginCriticalRegion() Notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception might jeopardize other tasks in the application domain. |
| 5 |
public static void BeginThreadAffinity() Notifies a host that managed code is about to execute instructions that depend on the identity of the current physical operating system thread. |
| 6 |
public static void EndCriticalRegion() Notifies a host that execution is about to enter a region of code in which the effects of a thread abort or unhandled exception are limited to the current task. |
| 7 |
public static void EndThreadAffinity() Notifies a host that managed code has finished executing instructions that depend on the identity of the current physical operating system thread. |
| 8 |
public static void FreeNamedDataSlot(string name) Eliminates the association between a name and a slot, for all threads in the process. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead. |
| 9 |
public static Object GetData(LocalDataStoreSlot slot) Retrieves the value from the specified slot on the current thread, within the current thread's current domain. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead. |
| 10 |
public static AppDomain GetDomain() Returns the current domain in which the current thread is running. |
| 11 |
public static int GetDomainID() Returns a unique application domain identifier. |
| 12 |
public static LocalDataStoreSlot GetNamedDataSlot(string name) Looks up a named data slot. For better performance, use fields that are marked with the ThreadStaticAttribute attribute instead. |
| 13 |
public void Interrupt() Interrupts a thread that is in the WaitSleepJoin thread state. |
| 14 |
public void Join() Blocks the calling thread until a thread terminates, while continuing to perform standard COM and SendMessage pumping. This method has different overloaded forms. |
| 15 |
public static void MemoryBarrier() Synchronizes memory access as follows − The processor executing the current thread cannot reorder instructions in such a way that memory accesses prior to the call to MemoryBarrier execute after memory accesses that follow the call to MemoryBarrier. |
| 16 |
public static void ResetAbort() Cancels an Abort requested for the current thread. Note: Obsolete in .NET Core and later versions. |
| 17 |
public static void SetData(LocalDataStoreSlot slot, Object data) Sets the data in the specified slot on the currently running thread, for that thread's current domain. For better performance, use fields marked with the ThreadStaticAttribute attribute instead. |
| 18 |
public void Start() Starts a thread. |
| 19 |
public static void Sleep(int millisecondsTimeout) Makes the thread pause for a period of time. |
| 20 |
public static void SpinWait(int iterations) Causes a thread to wait the number of times defined by the iterations parameter. |
Thread Lifecycle Visualization
Conclusion
The Thread class provides essential methods for thread management in C#. The most important methods are Start() to begin execution, Join() to wait for completion, and Sleep() to pause execution. Understanding these methods is crucial for effective multithreading in C# applications.
