Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between await and async and Task parallel library

What is the difference between Task Parallel Library and await and async. What was the need to introduce await and async? I see TPL is part of C# 4.0 and await/async is part of C# 5.0 but apart from that what is the basic difference. What was the need to introduce this new keyword?

like image 269
Joel Wilson Avatar asked Aug 31 '13 16:08

Joel Wilson


People also ask

What is the difference between task run and async-await?

Async methods are intended to be non-blocking operations. An await expression in an async method doesn't block the current thread while the awaited task is running. Instead, the expression signs up the rest of the method as a continuation and returns control to the caller of the async method.

What is Task Parallel Library?

The Task Parallel Library (TPL) is a set of public types and APIs in the System. Threading and System. Threading. Tasks namespaces. The purpose of the TPL is to make developers more productive by simplifying the process of adding parallelism and concurrency to applications.

Is async-await part of TPL?

Await & Async was built on the Task Parallel Library (TPL) which was introduced in the . NET Framework 4. Their purpose is to enable asynchronous programming.

What is async and await and how it difference from TPL in C#?

The purpose of TPL and the async-await features is to make the asynchronous programming almost as simple as the synchronous programming, allowing the developers to preserve the logical structure of the code even when it executes asynchronously.


1 Answers

The Task Parallel Library was designed for parallel programming - when you have a lot of work to do and want to split up that work among multiple threads so you can use all the CPU cores. TPL is best suited for CPU-intensive work.

Async and await are for asynchronous programming - when you have an operation (or many operations) that will complete in the future, and you want to do other things in the meantime. Async is best suited for I/O-bound work.

There is some overlap. For example, you can treat a parallel computation as an asynchronous operation so it doesn't tie up your UI thread. Also, both the TPL and async/await make use of the Task type, though they use it in very different ways.

like image 152
Stephen Cleary Avatar answered Oct 07 '22 02:10

Stephen Cleary