Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET 4.5 Async vs. TPL

I am starting to read about .NET 4.5 Async but frankly cannot get the most of it yet when to comes to the usage patter. So i will try to get that with a direct question:

I usually use .NET 4 TPL to call expensive Web services and DB calls from inside my ASP.NET application. Seems that i can achieve the same thing with Async. Is this true? When to use which?

Thanks in advance.

like image 575
mohamad halabi Avatar asked Nov 17 '11 14:11

mohamad halabi


1 Answers

TPL is a library for parallel computing. .NET 4.5 async is a language feature, built on top of TPL, that makes the process easier. This is especially true when you have workflows with multiple steps.

In a nutshell, async lets you write your code as if it were synchronous, so the logical flow remains intact. The process of waiting for a task to complete, running specific code when that happens, can be done in a very natural fashion with async. The C# 5.0 and VB 11.0 compilers transform your code into the equivalent C# 4.0 and VB 10.0 code using TPL and some new async related types.

For an excellent under-the-hood explanation of async, see Jon Skeet's Eduasync blog series.

So, how do you decide which to use? Well, async basically abstracts away all the complexities of creating a sequence of code fragments that are chained together with asynchronous calls. Presumably when you call a web service or access a database, you want to do something with what gets returned. async allows you to put the calling and processing code together, which should make your code easier to write and also easier read later on.

like image 200
Jeffrey Sax Avatar answered Dec 11 '22 07:12

Jeffrey Sax