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

44 / 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/Concurrency in C#
🔷C# Programming

Concurrency in C#

Updated 2026-05-15
10 min read

Concurrency in C#

Introduction

Concurrency is a fundamental concept in modern programming, allowing multiple tasks to be executed simultaneously. In the context of C#, managing concurrent operations efficiently can lead to significant performance improvements and better resource utilization. This tutorial will explore various aspects of concurrency in C#, including threading, asynchronous programming, and synchronization mechanisms.

Concept

Concurrency in C# is primarily managed through threads, which are lightweight units of execution that allow multiple tasks to run concurrently within a single process. The .NET framework provides several tools and libraries to facilitate concurrent programming, such as the System.Threading namespace, which includes classes for creating and managing threads, synchronization primitives, and asynchronous operations.

Key Concepts

  1. Thread: A thread is the smallest unit of execution that can be scheduled by an operating system.
  2. Asynchronous Programming: Asynchronous programming allows a program to perform other tasks while waiting for an operation to complete.
  3. Synchronization: Synchronization mechanisms ensure that shared resources are accessed in a controlled manner, preventing race conditions and data corruption.

Examples

1. Basic Threading

Let's start with a simple example of creating and starting a new thread in C#.

csharp
1using System;
2using System.Threading;
3
4class Program
5{
6 static void Main()
7 {
8 Thread thread = new Thread(new ThreadStart(PrintNumbers));
9 thread.Start();
10
11 for (int i = 1; i <= 5; i++)
12 {
13 Console.WriteLine("Main thread: " + i);
14 }
15 }
16
17 static void PrintNumbers()
18 {
19 for (int i = 1; i <= 5; i++)
20 {
21 Console.WriteLine("Thread: " + i);
22 }
23 }
24}

In this example, we create a new thread that executes the PrintNumbers method concurrently with the main thread. The output will show numbers printed alternately by both threads.

2. Asynchronous Programming

C# provides asynchronous programming support through the async and await keywords, which make it easier to write non-blocking code.

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

In this example, the PrintNumbersAsync method is marked as async, and it uses await Task.Delay to simulate a delay without blocking the main thread. The output will show numbers printed with a one-second interval.

3. Synchronization

Synchronization is crucial when multiple threads access shared resources. C# provides several synchronization primitives, such as Mutex, Semaphore, and Monitor.

csharp
1using System;
2using System.Threading;
3
4class Program
5{
6 static int counter = 0;
7 static readonly object lockObject = new object();
8
9 static void Main()
10 {
11 Thread thread1 = new Thread(IncrementCounter);
12 Thread thread2 = new Thread(IncrementCounter);
13
14 thread1.Start();
15 thread2.Start();
16
17 thread1.Join();
18 thread2.Join();
19
20 Console.WriteLine("Final counter value: " + counter);
21 }
22
23 static void IncrementCounter()
24 {
25 for (int i = 0; i < 1000; i++)
26 {
27 lock (lockObject)
28 {
29 counter++;
30 }
31 }
32 }
33}

In this example, we use a lock statement to ensure that only one thread can execute the critical section of code at a time, preventing race conditions and ensuring that the final counter value is correct.

What's Next?

In the next section, we will delve deeper into threading in C#, exploring more advanced topics such as thread pools, task parallel library (TPL), and concurrent collections. Understanding these concepts will help you write more efficient and scalable applications.

Stay tuned for more tutorials on C# concurrency!


PreviousAsync and Await KeywordsNext Threading in C#

Recommended Gear

Async and Await KeywordsThreading in C#