This tutorial aims to introduce you to the concept of parallel programming in C#. We'll break down tasks into independent subtasks that can be processed simultaneously to improve your application's performance.
By the end of this tutorial, you will:
- Understand the basics of parallel programming in C#
- Be able to write your own parallel programs in C#
- Know how to optimize your programs for better performance
This tutorial assumes you have a basic understanding of C# programming. If you're new to C#, you may want to first go through a basic C# tutorial.
Parallel programming is about executing multiple tasks at the same time. This is done by dividing a problem into discrete, independent tasks that can be executed in parallel.
In C#, the System.Threading.Tasks
namespace provides the Parallel
class, which has static methods for parallel implementations like For and ForEach loops.
Parallel.For
loopThe Parallel.For
loop works just like a regular for
loop, but the iterations are executed in parallel:
Parallel.For(0, 10, i => {
Console.WriteLine(i);
});
Parallel.ForEach
loopThe Parallel.ForEach
loop is a parallel version of the foreach
loop:
var numbers = new List<int> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Parallel.ForEach(numbers, number => {
Console.WriteLine(number);
});
Task
classThe Task
class represents a single operation that does not return a value and that usually executes asynchronously. Task instances are created by the Task.Factory.StartNew
method:
Task.Factory.StartNew(() => {
// Code to execute in parallel
});
Parallel.For
or Parallel.ForEach
loops when you can, because they're easier to use and read.Parallel.For
loopParallel.For(0, 10, i => {
Console.WriteLine($"Parallel.For: {i}");
});
This code will print the numbers from 0 to 9 in no particular order, because the iterations are being executed in parallel.
Parallel.ForEach
loopvar numbers = new List<int> {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Parallel.ForEach(numbers, number => {
Console.WriteLine($"Parallel.ForEach: {number}");
});
This code will print the numbers from the list in no particular order, because the iterations are being executed in parallel.
In this tutorial, you learned about parallel programming in C#. You learned how to use the Parallel.For
and Parallel.ForEach
loops and the Task
class to execute code in parallel.
To further your understanding of parallel programming in C#, you should try to apply these concepts in your own projects.
These exercises will help you practice the concepts you've learned.
Parallel.For
loop to print the numbers from 0 to 100.Parallel.ForEach
loop to print all the elements in a list of strings.Task
class to execute three different operations in parallel.Parallel.For(0, 101, i => {
Console.WriteLine(i);
});
var strings = new List<string> {"one", "two", "three", "four", "five"};
Parallel.ForEach(strings, str => {
Console.WriteLine(str);
});
Task.Factory.StartNew(() => {
Console.WriteLine("Task 1");
});
Task.Factory.StartNew(() => {
Console.WriteLine("Task 2");
});
Task.Factory.StartNew(() => {
Console.WriteLine("Task 3");
});
Try to use parallel programming in your own projects, and experiment with different numbers of tasks to see how it affects the performance.