codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🔷

C# Programming

45 / 60 topics
44Concurrency in C#45Threading in C#46Locks and Mutexes in C#47Memory Management in C#48Reflection in C#49Attributes in C#
Tutorials/C# Programming/Threading in C#
🔷C# Programming

Threading in C#

Updated 2026-05-15
10 min read

Threading in C#

Introduction

In the world of programming, especially in applications that require high performance and responsiveness, understanding how to effectively manage threads is crucial. Threads are the smallest units of execution within a process, allowing multiple operations to be performed concurrently. In this tutorial, we will explore the basics of threading in C#, including creating and managing threads.

Concept

Threading in C# allows you to run multiple tasks simultaneously. This can significantly improve the performance of your application by making better use of multi-core processors. The .NET framework provides several classes and mechanisms to work with threads, such as Thread, ThreadPool, and asynchronous programming models like async and await.

Key Concepts

  1. Thread: Represents a thread of execution within a process.
  2. ThreadPool: A pool of worker threads that can be used to execute tasks asynchronously.
  3. Synchronization: Mechanisms to prevent race conditions when multiple threads access shared resources.

Examples

Let's dive into some practical examples to understand how to create and manage threads in C#.

Creating a Thread

To create a thread, you need to define a method that the thread will execute. Here’s a simple example:

csharp
1using System;
2using System.Threading;
3
4class Program
5{
6 static void Main()
7 {
8 // Create a new thread
9 Thread thread = new Thread(new ThreadStart(PrintNumbers));
10
11 // Start the thread
12 thread.Start();
13
14 // Wait for the thread to finish
15 thread.Join();
16
17 Console.WriteLine("Main thread finished.");
18 }
19
20 static void PrintNumbers()
21 {
22 for (int i = 1; i <= 5; i++)
23 {
24 Console.WriteLine($"Number: {i}");
25 Thread.Sleep(1000); // Sleep for 1 second
26 }
27 }
28}

In this example, we create a new thread that executes the PrintNumbers method. The Thread.Start() method starts the execution of the thread, and Thread.Join() ensures that the main thread waits for the created thread to finish before it continues.

Using ThreadPool

The ThreadPool is a more efficient way to manage threads, especially when dealing with short-lived tasks. Here’s how you can use it:

csharp
1using System;
2using System.Threading;
3
4class Program
5{
6 static void Main()
7 {
8 // Queue work items to the thread pool
9 for (int i = 0; i < 5; i++)
10 {
11 ThreadPool.QueueUserWorkItem(new WaitCallback(PrintNumber), i);
12 }
13
14 Console.WriteLine("Main thread finished.");
15 }
16
17 static void PrintNumber(object state)
18 {
19 int number = (int)state;
20 Console.WriteLine($"Number: {number}");
21 Thread.Sleep(1000); // Sleep for 1 second
22 }
23}

In this example, we queue multiple work items to the thread pool. The ThreadPool.QueueUserWorkItem method takes a callback and an optional state object. Each work item is executed by a worker thread from the thread pool.

Asynchronous Programming

C# also provides asynchronous programming models like async and await, which simplify working with threads and I/O-bound operations:

csharp
1using System;
2using System.Threading.Tasks;
3
4class Program
5{
6 static async Task Main()
7 {
8 // Start an asynchronous task
9 await PrintNumbersAsync();
10
11 Console.WriteLine("Main thread finished.");
12 }
13
14 static async Task PrintNumbersAsync()
15 {
16 for (int i = 1; i <= 5; i++)
17 {
18 Console.WriteLine($"Number: {i}");
19 await Task.Delay(1000); // Delay for 1 second
20 }
21 }
22}

In this example, the PrintNumbersAsync method is marked with the async keyword, allowing us to use await to asynchronously wait for tasks. The Task.Delay method is used instead of Thread.Sleep to avoid blocking the thread.

What's Next?

After mastering threading in C#, you should explore more advanced topics such as Locks and Mutexes. These synchronization mechanisms are essential for preventing race conditions when multiple threads access shared resources. Understanding how to use locks and mutexes will help you write robust, concurrent applications.

By following this tutorial, you have gained a solid understanding of threading in C#. Whether you are building high-performance applications or simply looking to improve the responsiveness of your software, mastering threads is a valuable skill.


PreviousConcurrency in C#Next Locks and Mutexes in C#

Recommended Gear

Concurrency in C#Locks and Mutexes in C#