Task Parallelism C#
Task parallelism is the process of running tasks in parallel. Task parallelism divides tasks and allocates those tasks to separate threads for processing. It is based on unstructured parallelism. It means the parallel work unit may start and finish in places scattered according to the execution of the program.
In this article, we will be discussing different methods to achieve parallelism using Tasks in C#.
Note: If you are not familiar with Tasks in C#, you can learn more about it in my blog post C# Task.
Content
- Attaching Child Tasks To A Parent Task
- C# TaskFactory
- C# Task.WaitAll
- C# Task.WaitAny
- C# Parallel Class
- C# Parallel.For and Parallel.Foreach
- C# Parallel.Break
Attaching Child Tasks To A Parent Task
A Task can have several child Tasks. The parent Task finishes when all the child tasks are ready. Below code shows how this works.
The finalTask runs only after the parent Task is finished, and the parent Task finishes when all three children are finished. You can use this to create quite complex Task hierarchies that will go through all the steps you specified.
Note: Task.Run explicitly forbids having child tasks. Task.Run is the equivalent of Task.Factory.StartNew with the default scheduler and most importantly, TaskCreationOptions.DenyChildAttach. So any child task that attempts to execute as an attached child task (that is, it is created with the AttachedToParent option) will not be able to attach to the parent task and will execute instead as a detached child task.
C# TaskFactory
In the previous example, you had to create three Tasks all with the same options. To make the process easier, you can use a TaskFactory. A TaskFactory is created with a certain configuration and can then be used to create Tasks with that configuration. Below code shows how you can simplify the previous example with a factory.
C# Task.WaitAll
You can also use the method WaitAll to wait for multiple Tasks to finish before continuing execution. These are similar to Task.Wait, except they wait for multiple tasks to all complete.
Below code shows how to use this.
In this case, all three Tasks are executed simultaneously, and the whole run takes approximately 1000ms instead of 3000. Next to WaitAll, you also have a WhenAll method that you can use to schedule a continuation method after all Tasks have finished.
Task.WaitAll should be very rarely used. It is occasionally useful when working with Delegate Tasks, but even this usage is rare. Developers writing parallel code should first attempt data parallelism; and even if task parallism is necessary, then parent/child tasks may result in cleaner code than Task.WaitAll method.
C# Task.WaitAny
You can also use the WaitAny method to wait until one of the tasks is finished.
We can use Task.WaitAny when we have a collection of tasks, but we only interested in the first finished task. It can happen for example when we have a couple of async API that all of them do the same thing. But we want to receive the result from the one that returns the result first.
Also, you can create a WaitAllOneByOne pattern using the WaitAny method. It is useful when we would like to wait till all tasks finish, but process results as each one complete.
Below code shows how this works.
In this example, you process a completed Task as soon as it finishes. By keeping track of which Tasks are finished, you don’t have to wait until all Tasks have completed.
C# Parallel Class
The System.Threading.Tasks namespace also contains another class that can be used for parallel processing. The Parallel class has a couple of static methods—For, ForEach, and Invoke—that you can use to parallelize work.
Parallelism involves taking a certain task and splitting it into a set of related tasks that can be executed concurrently. This also means that you shouldn’t go through your code to replace all your loops with parallel loops. You should use the Parallel class only when your code doesn’t have to be executed sequentially.
Increasing performance with parallel processing happens only when you have a lot of work to be done that can be executed in parallel. For smaller work sets or for work that has to synchronize access to resources, using the Parallel class can hurt performance.
The best way to know whether it will work in your situation is to measure the results.
Below code shows an example of using Parallel.For and Parallel.ForEach.
C# Parallel.For and Parallel.Foreach
You can cancel the loop by using the ParallelLoopState object. You have two options to do this: Break or Stop. Break ensures that all iterations that are currently running will be finished. Stop just terminates everything. Here is an example:
C# Parallel.Break
When breaking the parallel loop, the result variable has an IsCompleted value of false and a LowestBreakIteration of 500. When you use the Stop method, the LowestBreakIteration is null.
Subscribe to Code with Shadman
Get the latest posts delivered right to your inbox