C
C#6mo ago
KidKai25

Advantage of async

I have actions methods like these
public ActionResult GetData1()
{
var data = FetchData1FromDatabase();
return Json(data, JsonRequestBehavior.AllowGet);
}

public ActionResult GetData2()
{
var data = FetchData2FromDatabase();
return Json(data, JsonRequestBehavior.AllowGet);
}

public ActionResult GetData3()
{
var data = FetchData3FromDatabase();
return Json(data, JsonRequestBehavior.AllowGet);
}
private List<MyData> FetchDataFromDatabase()
{
using (var db = new MyDbContext())
{
return db.MyData.ToList();
}
}
public ActionResult GetData1()
{
var data = FetchData1FromDatabase();
return Json(data, JsonRequestBehavior.AllowGet);
}

public ActionResult GetData2()
{
var data = FetchData2FromDatabase();
return Json(data, JsonRequestBehavior.AllowGet);
}

public ActionResult GetData3()
{
var data = FetchData3FromDatabase();
return Json(data, JsonRequestBehavior.AllowGet);
}
private List<MyData> FetchDataFromDatabase()
{
using (var db = new MyDbContext())
{
return db.MyData.ToList();
}
}
what happens if I dont make them async? Server can still process them concurrently right? By making a new thread.
9 Replies
Salman
Salman6mo ago
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
HtmlCompiler
HtmlCompiler6mo ago
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
Salman
Salman6mo ago
without async :
c#
var data = GetData();
//you can't do anything else now until Get Data finishes fetching data
c#
var data = GetData();
//you can't do anything else now until Get Data finishes fetching data
with async:
c#
Task<MyData> data = GetData();
//do other tasks when ready await the task
//all other tasks done
var myData = await data;
c#
Task<MyData> data = GetData();
//do other tasks when ready await the task
//all other tasks done
var myData = await data;
Just an example also it's highly recommended to use async with I/O operations like fetching data from the databases
KidKai25
KidKai25OP6mo ago
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.
ACiDCA7
ACiDCA76mo ago
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...
ACiDCA7
ACiDCA76mo ago
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
Salman
Salman6mo ago
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.
KidKai25
KidKai25OP6mo ago
Makes sense thanks!
Angius
Angius6mo ago
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"
Want results from more Discord servers?
Add your server