Understanding Threading and Tasks
Explore threading and tasks to manage parallel processing in C#.
Understanding Threading and Tasks is a free C# Academy lesson on CoddyKit — lesson 3 of 5. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 5 lessons in the course, and your progress syncs across the web and the CoddyKit app.
1
Understanding Threading and Tasks
Welcome to the next lesson! In this lesson, you’ll learn about threading and tasks in C#, which allow you to execute multiple operations concurrently for better performance. Let’s get started!

2
What is Threading?
Threading is the process of executing multiple operations simultaneously. Each thread represents a separate flow of control in your application.
Key Point: Threading improves performance by utilizing multiple processor cores for concurrent execution.
3
Creating and Starting Threads
You can create and start threads using the Thread class. Example:
using System;
using System.Threading;
class Program {
static void Main() {
Thread thread = new Thread(PrintNumbers);
thread.Start();
Console.WriteLine("Main thread is running...");
}
static void PrintNumbers() {
for (int i = 1; i <= 5; i++) {
Console.WriteLine(i);
Thread.Sleep(1000); // Pause for 1 second
}
}
}
4
Introduction to Tasks
Tasks provide a higher-level abstraction for concurrent programming compared to threads. The Task class is part of the Task Parallel Library (TPL) in C#.
Example: Creating and starting a task:
Tip: Tasks are easier to manage and provide better error handling compared to threads.
using System;
using System.Threading.Tasks;
class Program {
static void Main() {
Task task = Task.Run(() => {
for (int i = 1; i <= 5; i++) {
Console.WriteLine(i);
Task.Delay(1000).Wait();
}
});
Console.WriteLine("Main thread is running...");
task.Wait(); // Wait for the task to complete
}
}
5
Using Parallel.For and Parallel.ForEach
The Parallel class in C# allows you to execute loops in parallel for better performance. Example:
Key Point: Use Parallel.For and Parallel.ForEach for data-intensive tasks that can be executed concurrently.
using System;
using System.Threading.Tasks;
class Program {
static void Main() {
Parallel.For(0, 5, i => {
Console.WriteLine($"Processing {i} on thread {Task.CurrentId}");
});
Console.WriteLine("All tasks completed.");
}
}
6
Handling Exceptions in Tasks
You can catch exceptions in tasks using a try-catch block or the Task.Exception property. Example:
using System;
using System.Threading.Tasks;
class Program {
static void Main() {
try {
Task task = Task.Run(() => {
throw new InvalidOperationException("Task failed!");
});
task.Wait();
} catch (AggregateException ex) {
Console.WriteLine("Error: " + ex.InnerException.Message);
}
}
}
7
Practice Challenge
Create a program that starts multiple tasks to process numbers in parallel and prints their squares. Example:
using System;
using System.Threading.Tasks;
class Program {
static void Main() {
int[] numbers = { 1, 2, 3, 4, 5 };
Parallel.ForEach(numbers, number => {
Console.WriteLine($"Square of {number}: {number * number}");
});
}
}
8
9
Best Practices for Threading and Tasks
Here are some best practices:
- Prefer
TaskoverThreadfor high-level concurrency. - Use
Parallelclasses for data-intensive operations. - Handle exceptions properly to ensure robust applications.
- Limit the number of threads or tasks to avoid resource contention.
Tip: Use async programming when I/O operations dominate, and use threads/tasks for CPU-bound tasks.
10
Great Job!
Congratulations! You’ve learned the basics of threading and tasks for concurrent programming in C#. In the next lesson, we’ll dive into delegates and lambda expressions, two powerful features for functional programming. Let’s keep coding!

Frequently asked questions
Is the “Understanding Threading and Tasks” lesson free?
Yes — the full text of “Understanding Threading and Tasks” is free to read here on the web, and the C# Academy course includes 5 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “Understanding Threading and Tasks”?
Explore threading and tasks to manage parallel processing in C#. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 5, so you can start here or from the beginning and move at your own pace.
How long does the “Understanding Threading and Tasks” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Generics in C#
- Async and Await: Working with Asynchronous Code
- Understanding Threading and Tasks
- Exploring Delegates and Lambdas
- Understanding Reflection in C#