10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

Multithreading in C#

Updated on Sep 3, 2025
 
46,007 Views

Multithreading is a method to implement concurrent programming where multiple threads operate simultaneously. Threads are lightweight processes that signify the execution path in a program. Thread usage increases the efficiency of an application and reduces the CPU cycle time wastage.

The Thread Life Cycle

The thread life cycle encapsulates the life of a thread. It starts when an object of System.Threading.Thread class is created and ends when thread has terminated. The thread states in the thread life cycle are given as follows:

1. Unstarted State

When the instance of the thread has been created but the Start method has not been called, that is the unstarted state.

2. Ready State

When the thread is ready to run, it is in the ready state. This thread is just waiting for the CPU cycle.

3. Not runnable State

A thread can be in the not runnable state when the wait or sleep method has been called. This state can also occur if the thread is blocked because of I/O operations.

4. Dead State

The thread enters the dead state if it is aborted or if it has completed its execution.

Structure of Multithreading

Multithreading in C# involves running multiple threads concurrently within a single program, enhancing performance and efficiency. Here are the key components:

  1. Namespace: The System.Threading namespace provides classes and methods to create and manage threads
  2. Thread Class: A thread in C# is represented by the Thread class, where each thread executes a separate path of code
  3. Start Method: The Thread.Start method initiates a thread's execution
  4. Synchronization Mechanisms: Synchronization is crucial in multithreading to prevent data inconsistencies and race conditions, and C# provides mechanisms like locks, mutexes, and semaphores to manage access to shared resources

Example:

Thread thread = new Thread(new ThreadStart(TaskMethod));
thread.Start();

When and How to Use Multithreading in C#?

Multithreading in C# is beneficial when tasks are independent and can run concurrently, such as in

  1. CPU-bound operations
  2. I/O-bound tasks
  3. Maintaining application responsiveness during lengthy processes.

Consider an example of web server, when a web server receives multiple HTTP requests simultaneously, it can use multithreading to handle each request in a separate thread. This ensures that a slow request (e.g., a request that requires a database query) does not block other requests

Using multithreading in C# can significantly improve application performance and user experience

How to Use Multithreading:

To implement multithreading, start by creating a new thread using the Thread class from the System.Threading namespace. Define the task to be executed in a method and pass this method as a ThreadStart delegate to the Thread constructor. Invoke the Start method to run the thread. For example, Thread thread = new Thread(new ThreadStart(TaskMethod)); thread.Start();. Advanced methods include using the Task Parallel Library (TPL) and async and await keywords for simpler and more efficient multithreading management. Always ensure proper synchronization to avoid race conditions and deadlocks. Multithreading in C# should be used judiciously, keeping in mind the complexity it introduces to debugging and maintenance.

Steps to use Multithreading in C#

1. Creating a Thread:

Thread thread = new Thread(new ThreadStart(TaskMethod));
 thread.Start();

2. Using Task Parallel Library (TPL):

 Task.Run(() => TaskMethod());

3. Async/Await for Asynchronous Programming:

async Task MethodAsync() {
 await Task.Run(() => TaskMethod());
 }

C# Multithreading Best Practices

  1. Prefer Task Parallel Library (TPL): Simplifies thread management and improves efficiency.
  2. Use Async/Await: Enhances code readability and maintainability by avoiding callback hell.
  3. Proper Synchronization: Use locks, mutexes, and semaphores to manage shared resources and prevent race conditions.
  4. Avoid Excessive Locking: Prevents deadlocks and ensures better performance.
  5. Thread-Safe Collections: Utilize `System.Collections.Concurrent` for safe handling of collections in a multithreaded environment.
  6. Keep Tasks Small: Break down tasks to maximize concurrency and improve performance.
  7. Handle Exceptions: Implement proper exception handling within threads to ensure robustness.
  8. Limit Thread Creation: Avoid creating too many threads to prevent resource exhaustion.

Regular testing and profiling are essential to identify and resolve issues in multithreaded applications..

Example Program on Multithreading

Here's a simple example demonstrating multithreading in C#:

using System;
using System.Threading;
class Program {
 static void Main() {
 Thread thread1 = new Thread(PrintNumbers);
 Thread thread2 = new Thread(PrintNumbers);
 thread1.Start();
 thread2.Start();
 thread1.Join();
 thread2.Join();
 }
 static void PrintNumbers() {
 for (int i = 0; i < 5; i++) {
 Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}: {i}");
 Thread.Sleep(500); // Simulate work
 }
 }
}

Explanation:

  1. Namespace Imports: Includes `System` and `System.Threading`.
  2. Main Method:
    a. Thread Creation: Creates two new threads (`thread1` and `thread2`) with the `PrintNumbers` method as their target.
    b. Start Threads: Initiates both threads using `Start()` method.
    c. Join Threads: Waits for both threads to complete using `Join()` method, ensuring the main thread does not exit prematurely.
  3. PrintNumbers Method:
    a. Loop Execution: Iterates five times, printing the thread ID and loop counter.
    b. Thread.Sleep(500): Simulates work by pausing execution for 500 milliseconds between iterations.

This example shows how two threads execute the same method concurrently, improving performance.

The Thread Class

The System.Threading namespace provides the classes and interfaces that implement multithreaded programming. One of the classes in this namespace is the Thread class. This class is used for creating and controlling threads.

Details about the constructors, properties and methods in Thread Class are given as follows:

Constructors in Thread Class

The different constructors and their description is given as follows:

Constructors

Description

Thread(ParameterizedThreadStart)

This constructor initializes a new instance of the Thread class, specifying a delegate that allows an object to be passed to the thread when the thread is started.

Thread(ParameterizedThreadStart, Int32)

This constructor initializes a new instance of the Thread class, specifying a delegate that allows an object to be passed to the thread when the thread is started and specifying the maximum stack size for the thread.

Thread(ThreadStart)

This constructor initializes a new instance of the Thread class.

Thread(ThreadStart, Int32)

This constructor initializes a new instance of the Thread class, specifying the maximum stack size for the thread.

Table: Constructors in Thread Class in C#
Source: MSDN

Properties in Thread Class

The different properties and their description is given as follows:

Properties

Description

ApartmentState

This property gets or sets the apartment state of this thread.

CurrentContext

This property gets the current context in which the thread is executing.

CurrentCulture

This property gets or sets the culture for the current thread.

CurrentPrincipal

This property gets or sets the thread's current principal (for role-based security).

CurrentThread

This property gets the currently running thread.

CurrentUICulture

This property gets or sets the current culture used by the Resource Manager to look up culture-specific resources at run time.

ExecutionContext

This property gets an ExecutionContext object that contains information about the various contexts of the current thread.

IsAlive

This property gets a value indicating the execution status of the current thread.

IsBackground

This property gets or sets a value indicating whether or not a thread is a background thread.

IsThreadPoolThread

This property gets a value indicating whether or not a thread belongs to the managed thread pool.

ManagedThreadId

This property gets a unique identifier for the current managed thread.

Name

This property gets or sets the name of the thread.

Priority

This property gets or sets a value indicating the scheduling priority of a thread.

ThreadState

This property gets a value containing the states of the current thread.

Table: Properties in Thread Class in C#
Source: MSDN

Methods in Thread Class

The different methods and their description is given as follows:

Methods

Description

Abort()

This method 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.

BeginCriticalRegion()

This method 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.

BeginThreadAffinity()

This method notifies a host that managed code is about to execute instructions that depend on the identity of the current physical operating system thread.

EndCriticalRegion()

This method 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.

EndThreadAffinity()

This method notifies a host that managed code has finished executing instructions that depend on the identity of the current physical operating system thread.

Finalize()

This method ensures that resources are freed and other cleanup operations are performed when the garbage collector reclaims the Thread object.

GetDomain()

This method returns the current domain in which the current thread is running.

GetType()

This method gets the Type of the current instance.

Interrupt()

This method interrupts a thread that is in the WaitSleepJoin thread state.

MemoryBarrier()

This method 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().

Resume()

This method resumes a thread that has been suspended.

Sleep(Int32)

This method suspends the current thread for the specified number of milliseconds.

Start()

This method causes the operating system to change the state of the current instance to Running.

Suspend()

This method either suspends the thread, or if the thread is already suspended, has no effect

VolatileRead(Byte)

This method reads the value of a field. The value is the latest written by any processor in a computer, regardless of the number of processors or the state of processor cache.

VolatileWrite(Byte, Byte)

This method writes a value to a field immediately, so that the value is visible to all processors in the computer.

Yield()

This method causes the calling thread to yield execution to another thread that is ready to run on the current processor. The operating system selects the thread to yield to

Table: Methods in Thread Class in C#
Source: MSDN

Create a Thread

To create a Thread, simply use the Thread class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
class Example
{
   public void FirstThread()
   {
       for (int i = 0; i < 5; i++)
       {
           Console.WriteLine("My Thread");
       }
   }
}
class Sample
{
   public static void Main()
   {
       Example e = new Example();
       Thread t = new Thread(new ThreadStart(e.FirstThread));
       t.Start();
       Console.Read();
   }
}

Source Code: Program to create a Thread in C#

The above program generates the following output:

My Thread
My Thread
My Thread
My Thread
My Thread

The Main Thread

The Main Thread is the first thread that is created and executed inside a process. It is automatically created when the process starts execution.

A program that demonstrates the Main thread is given as follows:

using System;
using System.Threading;
namespace MultithreadingDemo
{
  class Example
  {
     static void Main(string[] args)
     {
        Thread myThread = Thread.CurrentThread;
        myThread.Name = "Main_Thread";
        Console.WriteLine("This thread is the {0}", myThread.Name);
     }
  }
}

Source Code: Program to implement Main Thread in C#

The output of the above program is as follows:

This thread is the Main_Thread

Now let us understand the above program.

The CurrentThread property of the Thread class is used to access the Main thread. The name of the thread is specified is Main_Thread. After that, this is displayed. The code snippet for this is given as follows:

    Thread myThread = Thread.CurrentThread;
        myThread.Name = "Main_Thread";
        Console.WriteLine("This thread is the {0}", myThread.Name);

Check status of Current Thread

The isAlive property is to be used if you want to check the status of current Thread in C#.

Let us see an example:

using System;
using System.Threading;
namespace Example
{
   class MyThread
   {
       static void Main(string[] args)
       {
           Thread t = Thread.CurrentThread;
           t.Name = "This is my first thread!";
           Console.WriteLine("Status of current thread = {0}", t.IsAlive);
           Console.ReadKey();
       }
   }
}

Source Code: Program to check the status of current Thread in C#

The above program generates the following output:

Status of current thread = True

Get the name of Current Thread

To get the name of the current thread, you need to use the Name property of the Thread class. Let us see an example:

using System;
using System.Threading;
namespace Demo
{
   class MyClass
   {
       static void Main(string[] args)
       {
           Thread t = Thread.CurrentThread;
           t.Name = "First Thread";
           Console.WriteLine("Name of thread : {0}", t.Name);
           Console.ReadKey();
       }
   }
}

Source Code: Program to get the name of current Thread in C#

The above program generates the following output:

Name of thread : First Thread

Display the priority of the Current Thread

The Priority property of the Thread class is to be used for displaying the priority of the Current Thread:

using System;
using System.Threading;
namespace Example
{
   class MyThreadClass
   {
       static void Main(string[] args)
       {
           Thread t = Thread.CurrentThread;
           t.Name = "First Thread";
           Console.WriteLine("Thread Name : {0}", t.Name);
           Console.WriteLine("Thread Priority : {0}", t.Priority);
           Console.ReadKey();
       }
   }
}

Source Code: Program to display the priority of current Thread in C#

The above program generates the following output:

Thread Name : First Thread
Thread Priority : Normal

Pause the Thread for a specified period

Use the Sleep() method to pause the Thread for a specified period. Let us see the code now:

using System;
using System.Threading;
namespace Example
{
   class MyThreadClass
   {
       static void Main(string[] args)
       {
           Thread t = Thread.CurrentThread;
           t.Name = "First Thread";
           Console.WriteLine("Thread Name : {0}", t.Name);
           Console.WriteLine("Thread Priority : {0}", t.Priority);
           Console.WriteLine("Child Thread Paused...");
           // using Sleep() method
           Thread.Sleep(1000);
           Console.WriteLine("Child Thread Resumed...");
           Console.ReadKey();
       }
   }
}

Source Code: Program to pause the Thread for a specified period in C#

The above program generates the following output:

Thread Name : First Thread
Thread Priority : Normal
Child Thread Paused...
Child Thread Resumed...

Conclusion

Multithreading in C# significantly enhances application performance and responsiveness by executing tasks concurrently. Proper implementation and adherence to best practices, including synchronization and efficient use of TPL and async/await, are crucial. By leveraging multithreading, developers can create scalable, responsive applications. However, it's essential to manage the complexity and potential issues associated with concurrent execution. Regular testing and profiling ensure smooth and efficient performance.

+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses