Running multiple actions asynchronously using tasks

The Thread class has been available since the first version of C# and can be used to create new threads and manage them, but it can be tricky to work with directly.

C# 4 introduced the Task class, which is a wrapper around a thread that enables easier creating and management. Creating multiple threads wrapped in tasks will allow our code to execute asynchronously (at the same time).

Each Task has a Status property and a CreationOptions property, has a ContinueWith method that can be customized with the TaskContinuationOptions enum, and can be managed with the TaskFactory class, as shown in the following diagram:

We will look at three ways to start the methods using Task instances. Each ...

Get C# 7.1 and .NET Core 2.0 – Modern Cross-Platform Development - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.