Shadman Kudchikar

Introduction To Parallel Programming Using C# Tasks


Why We Need Async And Parallel Programming In C#?

  • Responsiveness: We want to hide the latency of possibly long-running or blocking operation by starting the operation in the background. This we refer to asynchronous programming or async.

  • Performance: We want to reduce the time of execution of CPU bound operation by dividing them into the chunk of operation and executing them in parallel. This we refer to parallel programming.

Why Task In C#?

We have seen different methods to handle async and parallel operations in tutorial Multithreading in C#. This methods are, Thread Class and Thread Pool.

Also, there are various other methods like Async Programming Model and Event-Based Async Pattern.


Why We Need Task-based Asynchronous Pattern In C#?

The Task-based Asynchronous Pattern (TAP) is based on the System.Threading.Tasks.Task and System.Threading.Tasks.Task<TResult> types in the System.Threading.Tasks namespace, which are used to represent arbitrary asynchronous operations. TAP is the recommended asynchronous design pattern for new development.

Tasks provide a sophisticated way to handle async or parallel operation by providing various options like,

  • Ability to cancel an ongoing operation
  • Return resulting value from operation (like a method functions)
  • Easy Exception Handling
  • High-Level constructs like a parallel loop
  • task continuation

and much more…

What is C# Task?

What Microsoft says,

“Task represents an asynchronous operation.”


“Task objects are one of the central components of the task-based asynchronous pattern first introduced in the .NET Framework 4. Because the work performed by a Task object typically executes asynchronously on a thread pool thread rather than synchronously on the main application thread, you can use the Status property, as well as the IsCanceled, IsCompleted, and IsFaulted properties, to determine the state of a task. Most commonly, a lambda expression is used to specify the work that the task is to perform.”

What I say,

“Task can be considered a unit of work or ongoing operation in a separate thread”.


We will understand this concept better once we dive deep and look at some examples in the next chapter.

comments powered by Disqus