Async vs Sync Methods

Bhrugen Patel
A free video tutorial from Bhrugen Patel
Passionate Software Developer
4.5 instructor rating • 21 courses • 84,024 students

Learn more from the full course

The complete ASP.NET MVC Core 2.0 Course

Learn basic fundamentals and advance concepts of ASP.NET Core 2.0 as we build multiple projects

06:48:58 of on-demand video • Updated

  • Learn structure of ASP NET MVC Core 2 Project
  • Learn basic fundamentals of ASP NET MVC Core 2
  • Build applications using ASP NET MVC Core 2
  • Build applications using ASP NET MVC Core 2 using Razor Pages
  • Build 3 Projects throughout the course
  • Integrate Identity Framework and learn how to add more fields to Users
  • Integrate Entity Framework along with code first migrations
  • Tag Helpers in MVC Core 2.0
English [Auto] In this video we will take a look at synchronous and asynchronous programming and understand what's the difference between them. So first synchronous programming is the traditional programming that we have been doing so far. So what will happen is that every step will be executed line by line. So if you see in the example here you see we have three methods to get content get out and get a name and then each one of them we are executing the command Thread. Sleep for a specific amount of time. Two seconds five seconds and three seconds. So now when they would be called in the call right here at the bottom you see we are calling all the three methods here. So what will be the total time to execute all three matters here. That will be the total of all of this. What does sleep. So it will be somewhat around 10 seconds because if you add all three it runs up to 10 seconds. So when you execute something synchronously you will wait for it to finish before moving on to another task. And every time a method is called It will wait for a few seconds as you know there are as your program has So in this example the services that get count cannot be called before they get content returns the value and it is stored in the context really about here. Now lets take a look at what would happen if we did asynchronous programming. So when you execute something asynchronously you can move on to another task before it finishes. Now in C-sharp you will have to use a wait and is the key words in order to make a function asynchronous. So if you see we have again those same matters here and in that method the only thing different is we have a async key word. And then there is task and the return type. Now you need this task a keyword to denote what will be the return type of this function. And finally whenever we are calling anything we have to await key word. So that means that they'll have to operate this. So all of the time delays that we have they are all waiting here. And then if we take a look at the execution or when will be called we see that whenever we are calling this functions restoring them in the way people here. And then we are awaiting that retrievable and then storing them in the final content count and name wait people. So what will happen in this case is that first to get content in sync will be called. And then there is a delay. So what will happen is that it will automatically go to the next one and call the get count in sync. And then again there is a delay so it will lead to execution rate. But in the meanwhile it will call the third function. We just get name Issing. So let's say all of these three functions get caught instantaneously without having wait on two seconds five seconds or three seconds. And because of that it increases the efficiency. Now what would happen is here we have a weak keyword. So this means when the execution reaches this statement it will have to wait until the content task returns a value. So as soon as content tax returns a value from the matter after waiting for two seconds it will populate in the way DBO content. After that it will look for the current task and once the count is incomplete after five seconds it will populate here. And finally when name task get a value from get name a sink it will start there in the name. So the total time taken for these executions will be about five seconds. And that is because we do not have to wait two seconds and then execute get conducing and then execute the get name Issing all the three methods will be called instantaneously one after each other and it will be in microseconds. And because the maximum weight is in the GET count async for five seconds the total execution time will be about five seconds for all three methods. So asynchronous operations are not faster but they are more efficient because of which they execute faster. If there is just one matter in both the examples we discussed with the sleep time of 10 seconds then it would take the same time for synchronous and asynchronous executions. But since we have multiple matters in asynchronous it would not wait for one operation to finish before it kicks off the other operation. And another key factor to notice is that in C-sharp in order to implement asynchronous you need async keyword and await keyword and also the return type should be enclosed within task. This was just an overview because understanding these basic concepts would help you increase the efficiency of your project drastically.