Using tasks

A task represents an operation that may or may not return a value and executes asynchronously. Since they are executed asynchronously, they are executed as worker threads from the thread pool rather than the primary thread. This allows us to use the isCanceled and IsCompleted properties to understand the state of the task. You can also make a task run synchronously, which will be executed on the main or primary thread.

A task can implement the IAsyncResult and IDisposable interfaces like so:

public class Task : IAsyncResult, IDisposable

Let's look at an example so that we can understand how we can create and initiate a task in different ways. In this example, we will use an action delegate that takes an argument of the object ...

Get Programming in C#: Exam 70-483 (MCSD) Guide 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.