![]() |
VOOZH | about |
The Thread class in C# is part of the System.Threading namespace and provides the fundamental way to create and control threads. A thread represents a path of execution within a process, and using the Thread class you can start, pause, resume, or terminate tasks running concurrently with the main program.
Output:
Worker Thread: 1
Main Thread: 1
Worker Thread: 2
Main Thread: 2
...
Explanation:
Thread t = new Thread(MethodName);
t.Start();
Here:
The Thread class allows passing data through ParameterizedThreadStart or lambda expressions.
Output:
Message: Hello from Thread
The Thread class provides multiple constructors to create and initialize threads with or without parameters, and with optional stack size specification.
| Constructor | Description |
|---|---|
| Thread(ParameterizedThreadStart) | Initializes a new instance of the Thread class, specifying a delegate that allows an object to be passed to the thread when it starts. |
| Thread(ParameterizedThreadStart, int) | Creates a new Thread instance with a delegate for object passing and sets the maximum stack size. |
| Thread(ThreadStart) | Initializes a new instance of the Thread class without parameters. |
| Thread(ThreadStart, int) | Initializes a new instance of the Thread class, specifying the maximum stack size. |
Example: Using ThreadStart Constructor
My Thread is in progress...!! My Thread is in progress...!!
| Property | Description |
|---|---|
| ApartmentState | Gets or sets the apartment state of this thread. |
| CurrentContext | Gets the current context in which the thread is executing. |
| CurrentCulture | Gets or sets the culture for the current thread. |
| CurrentPrincipal | Gets or sets the thread’s current principal (used for role-based security). |
| CurrentThread | Gets the currently running thread. |
| CurrentUICulture | Gets or sets the culture used by the ResourceManager to look up culture-specific resources at runtime. |
| ExecutionContext | Gets an ExecutionContext object containing information about the various contexts of the current thread. |
| IsAlive | Gets a value indicating the execution status of the current thread. |
| IsBackground | Gets or sets a value indicating whether a thread is a background thread. |
| IsThreadPoolThread | Gets a value indicating whether a thread belongs to the managed thread pool. |
| ManagedThreadId | Gets a unique identifier for the current managed thread. |
| Name | Gets or sets the name of the thread. |
| Priority | Gets or sets the scheduling priority of a thread. |
| ThreadState | Gets a value containing the states of the current thread. |
The priority of Thread 1 is: Normal The priority of Thread 2 is: Lowest The priority of Thread 3 is: AboveNormal
| Method | Description |
|---|---|
| Start() | Begins execution of the thread. |
| Sleep(Int32) | Suspends the current thread for a specified time. |
| Join() | Blocks the calling thread until the target thread finishes. |
| Abort() | Terminates a thread (obsolete in modern .NET, not recommended). |
| Interrupt() | Interrupts a thread in Wait, Sleep, or Join state. |
| Suspend() | Suspends a thread (obsolete, not recommended). |
| Resume() | Resumes a suspended thread (obsolete, not recommended). |
| Yield() | Temporarily releases the processor to allow other threads to run. |
| SpinWait(Int32) | Makes the thread wait actively for a set number of iterations. |
| Method | Description |
|---|---|
| GetApartmentState() / SetApartmentState(ApartmentState) | Gets or sets the apartment state of a thread. |
| AllocateDataSlot() / AllocateNamedDataSlot(String) | Creates thread-local data slots. |
| GetData(LocalDataStoreSlot) / SetData(LocalDataStoreSlot, Object) | Stores or retrieves thread-specific data. |
| FreeNamedDataSlot(String) | Frees a named data slot. |
| BeginCriticalRegion() / EndCriticalRegion() | Marks a region where aborts or exceptions affect the application domain. |
| BeginThreadAffinity() / EndThreadAffinity() | Marks code that depends on the identity of the physical OS thread. |
| MemoryBarrier() | Ensures correct ordering of memory operations across threads. |
| VolatileRead() / VolatileWrite() | Provides thread-safe memory read/write operations. |
Output:
Task1 running...
Task1 running...
Task1 running...
Main thread finished