Relationships between parallel tasks
In the previous chapter, Chapter 5, we learned how to use async
and await
to perform work in parallel and manage the flow of tasks by using ContinueWith
. In this section, we will examine some of the TPL features that can be leveraged to manage relationships between tasks running in parallel.
Let’s start by looking deeper into the Parallel.Invoke
method provided by the TPL.
Under the covers of Parallel.Invoke
In Chapter 2, we learned how to use the Parallel.Invoke
method to execute multiple tasks in parallel. We are going to revisit Parallel.Invoke
now and discover what is happening under the covers. Consider using it to invoke two methods:
Parallel.Invoke(DoFirstAction, DoSectionAction);
This is what is happening behind the scenes:
List<Task> taskList = new();
taskList.Add(Task.Run(DoFirstAction));
taskList.Add(Task.Run(DoSectionAction));
Task.WaitAll(taskList.ToArray());
Two tasks will be created and queued...