Advantage of async
I have actions methods like these
what happens if I dont make them async?
Server can still process them concurrently right? By making a new thread.
9 Replies
the main thread will be blocked until those operations are completed and you can't do anythign else meanwhile ..without async
also async is a better solution than making a new thread I believe
some of those sync requests could technically run contemporaneously, i think
there's still a thread pool behind that
although i wouldn't necessarily count on that and it's a deprecated practice
without async :
with async:
Just an example
also it's highly recommended to use async with I/O operations like fetching data from the databases
Just deprecated practice? That's it?
You can see my code I am not doing any other tasks in between. You didn't really say anything I didn't know and answered the question what's wrong with my current code.
Using Asynchronous Methods in ASP.NET 4.5
This tutorial will teach you the basics of building an asynchronous ASP.NET Web Forms application using Visual Studio Express 2012 for Web, which is a free...
tldr:
with not using async you are blocking threads that could be used to process more requests
so as long you arent starved for resources, it doesnt matter much but as soon you are under load you might timeout
Ofc in the above given code you aren't doing any other operation because it's just a function for fetching data. But you would consume this function somewhere and there you might have other stuff going on and you won't want to block the thread until your data finishes fetching. So think from that perspective where you would consume this function. Like I showed you an example above.
Makes sense thanks!
Sync:
* User A requests a blogpost
* User B requests a user profile
* Application says "can't fetch the user profile, I'm waiting for the database to give me blogpost, wait"
Async:
* User A requests a blogpost
* User B requests a user profile
* Application says "I'm waiting for the database to get me the blogpost, so sure, I'll fetch the profile in the meantime"