Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When and why would you not use task-base asynchronous programming in C# 5.0 ASP.NET MVC 4 or Web API

I'm trying to get my head around asynchronous programming in C# 5 and the question that I keep asking myself is why not use it all the time (for ASP.NET MVC 4 or Web API), like the node.js guys are doing?

Is there any disadvantages (unnecessary overhead) other than the code not as straightforward (which is IMHO a joke since the new syntax is great an easy to understand)?

like image 399
W3Max Avatar asked Mar 20 '12 01:03

W3Max


1 Answers

For the most part, use it everywhere indeed. That is why they turned everything that takes longer than 50 ms into an async call :).

However, it will have the same pitfalls as any async code. Debugging could become trickier. If you begin doing one thing and then it returns to a long task on the UI thread, it could result in false freezing (user was able to move after click, but then later cannot when doing nothing as far as they see it). Etc

If you run it through code that waits for values, then it will act like synchronous code anyway. And, if it hits the cache or some fast action, then it will not run async for efficiency. So, this last point means that even though there is overhead to perform compiler magic in these calls, that magic will not be done if the call is completed already and therefore not worth the overhead.

So, yes, I would say use it just about everywhere :) It is a great step for .NET

UPDATE

If this article is to believed then it only justifies what I am saying. If the call takes less than 50 ms, then it is not and should not be async due to the overhead to write it that way. And, in fact will most often trigger the synchronous action instead of the async one. However, if it is over 50 ms, then you only gain from the async call

like image 65
Justin Pihony Avatar answered Oct 21 '22 18:10

Justin Pihony