Async/Await an Introduction (Asynchronous programming in .NET 5.0)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone and welcome to dot net course central in today's video i am going to walk through the basics of async and await async and await keyword were introduced in c sharp and it is part of the task asynchronous programming model or for short tap model now pre async await if we have to write asynchronous code we would either use a thread class or we'll use a thread pull and then programming with thread class and thread pull does similar job but going through the code reading and understanding was a little bit complex as well as how you debug with async innovate the programming model becomes much more simpler and i'm going to demonstrate that but for example what i have taken today is a use case of a driver so when a driver in our case let's consider a distribution driver goes into a distribution center after the driver reaches the distribution center the load will be verified by a load verification manager and then if the load verification is correct then a new load will be assigned to the driver and in the meantime if the driver belongs to a separate company they will also inform their back office about reaching the distribution center and some other information now as a part of that if we consider this as a software then the distribution center is going to have a load verification system and new load assignment system and similarly driver will work with the back of the system on their side now if we define that in a class model then we can have a driver class which has report to back office a load verifier class which is verifying the load and the new load assignment class which is assigning new load now for these three classes for the time being i just added thread.sleep and we're sleeping for five seconds for all these functions this is just to start and then slowly we will change the code to have a sync await implementation to start what i'm going to do is i'm going to create an instance of the three classes and then i'm going to execute all the three method which are going to happen in sequence and then i'm going to print out the time taken for the overall operation after i create the instance of these three classes i'm going to call driver dot report to back office then verifier dot verify and then finally assigner dot assign now at this point in time i'm not checking if verification was successful or not i'm just going to check all the three methods and see how much time it takes just to execute the three method in sequence once i run this project it should print out total time is a little bit more than 15 seconds there will be few milliseconds for executing but plus minus 15 seconds is what it is going to print out so i'm going to run this application and i'm going to fast forward these two where it prints out the output and as you can see it is taking 15 seconds and few milliseconds which is totally expected now if we want to run them in parallel what we can do well that's where async await would come in handy but before we get into async await let's instead just create a task and run them in parallel so we can say task dot run and similarly i'm going to run this three now if i just run this application right now it's going to execute immediately and it is not going to wait the reason for that of course is though i'm doing a task.run but i am not awaiting on the task that is returned just to show that i'm going to run it and you'll see that it came out immediately now what i can do is i can do a wait here since it is returning a task i can do a wait and here i have to change this to a sync task instead of what now if i run what is the expectation the expectation is it is going to execute all the three in parallel and reduce the time to five seconds now it is still taking 15 seconds why well the reason is simple though they are running in parallel but we are waiting on each task and then going into the next task we are not just executing them parallel we are still doing the await serially and that's the problem and that's where i personally have couple of times than similar mistake where i have got an async function and then i call two function back to back with a wait statement which doesn't give any benefit in terms of asynchronicity but before i go and rewrite this code i am going to change the implementation of the classes so that we can start from await and async all the way up so here first of all i'm going to go into the interface and change name to async because that's the naming convention and here it's going to be async task and in this place instead of thread.sleep i'm going to say task dot delay and i'm going to await on the task and here also it's going to be task similarly i'm going to go into the load designer and i'm going to rename this to assign a sync and instead of void it's going to be task and then same for i load verifier it's going to be verify sync and instead of pull it's going to be task of bull now it's time to change the implementation here and we can have task pull and here we'll have task dot delay and we'll have an evade and a sync and then finally here have same same task and here we're going to have task dot delay and we'll have an event in front of it and now we're going to go into the program and get rid of all these and just i'm going to run it once again just to make sure everything is working as expected and we are still seeing 15 second as output and as we can see is still 15 seconds now since our goal is to run them in parallel we cannot evade all of them one after another instead what we are going to do is we are going to say driver task is equal to this and then verify task is equal to this and then finally assign a task is equal to this and then we are going to do a task dot wait all and here we are going to pass the driver task the verifier task and the assigner task all the three tasks and now if we run the application we are going to see everything is going to execute in 5 second instead of 15 seconds because at this point in time we are executing all of them in parallel and not in sequence previously when we were doing a weight on each statement we are practically waiting on all each one of them to complete and then go to the next step now when we do task dot wait all all the tasks are going to start it at the same time so that's the fundamental difference and now you see it executed in five second as we expected now this is still not done because in real life the driver reporting to back office and verify can happen in parallel but for assigned task we want to wait so there are two ways of doing it first of all here we can we can do a task dot wait all we can finish the first two tasks fast and then finally we can await on the assignment that's one way of doing so this way these two tasks will trigger the driver back office report and the verify task will trigger in parallel so they both of them will be done in five seconds and then finally assignment will happen which will take another five second the reason we want to do it this way because the assignment depends on whether the verify task was completed or not and here we are going to do we can just do if the result is true then do it otherwise do something in our case it will always be true because we have hard-coded the response to true so i'm just going to keep the code simple and this is what we are going to do so now if we run the application it should take 10 seconds instead of 5 seconds so this is the output now one thing worth mentioning is the reason i am doing this dot delay is i'm trying to mimic an http call here so if we have an http call it can take few milliseconds up to some seconds so i'm just mimicking uh http call and the reason i gave five second it's to just to show in the console output a proper number that's about it but in real life if you make an http call it can go from few hundred millisecond to up till couple of seconds depending on how fast or slow the service is so this is the reason i just added earlier it was thread dot sleep and now it is touch dos delay just to mimic an http call now here the other thing we can do is instead of task dot wait all there is another method we can use is when all so the difference between task dot wait all and task dot when all is touched or when all itself returns another task so you have to await on it whereas stars dot wait all did not that's the difference and this one creates a task that will complete when all of the task object in the array is completed and returner tasks represent the completion of all tasks applied but if i if i go back and if i do a wait all wait all does the same thing waits for all provided tasks to conflict execution it's just that it does not return a task back it's about it that's the only difference so now if we run this application it should yield the same result as before which is 10 seconds the other thing we can do is let's say we want to know when each of this driver task or verify task is completed want to do some sort of print out so in that case what we can do is we can create an area of task so task is equal to and here we can say driver task and verified task and here after that what we can do is we can say while does dot count is greater than zero then equal to and here we can say await task dot when any which is going to return as soon as at least one of the task is completed and if we say if task is equal to driver task then writeline driver task completed and then we can say else we have only two tasks so i don't have to do any other condition i can just copy paste here and we can say verify task completed and after this we can remove the completed task from the list of tasks so that way the same task it's not going to wait on a completed task and it will not create an issue so now if we do that we don't need this anymore because it's going to do the exact same thing but we will be able to print out which task completed this is really useful when you want to add some trace initial days of development this is really useful and we can see verify completed driver completed and then finally it's going to show 10 second because the other one also completed after 10 seconds so this video i tried to cover very basic of async anyway and as i mentioned i in these functions i was just trying to mimic an http client call or any io operation which your cpu bound and as you have seen during execution with the async and await instead of blocking on the call the call always comes back to the caller which is main in this case and then we can evade or perform other tasks so as you can see that here when we call this async both of the session method the call immediately comes back to the caller and goes to the next step and the blocking will happen only on the await statement but before that execution will continue to do the work that it has to do so for example here if we had a for statement and let's say 20 and we just do just print this out this will continue to print as the tasker executed in the background and then after this printout is done it's going to come here and it will get into wait any whereas the actual time taken by the task is not going to change it is still going to take 10 seconds to complete it because this execution of this for loop is happening in the main thread whereas the other two tasks are running on background threads so to demonstrate that i'm going to run this and you can see yeah this is done from 0 to 19 we're still waiting the task driver task and verified tasks are completed and then it still takes 10 seconds now what we can do is if we do uh here if we do a thread dot sleep and let's say we give a sleep off so we have five seconds so let's give one second will be too much so let's give four let's do a loop of four and give a second each time so we will be using four seconds in this loop but we should see immediately after that this should execute and total time taken should still be 10 seconds and not 14 seconds so let's do that let's try this out as well and we see the total time taken is still 10 seconds this is just to show that after you execute couple of tasks or couple off after you make a call to couple of async method you can continue doing some processing cpu intensive processing if needed and then you can wait on your statement and this will create a appropriate parallel execution so that's all i wanted to cover today if you like this video please give me a thumbs up and if you have any question regarding what i have covered today please leave me a comment and i will surely answer to best of my knowledge and if you are new to my channel and if you think you are going to get value out of my channel please subscribe to my channel and thanks so much for watching this video
Info
Channel: DotNet Core Central
Views: 11,469
Rating: undefined out of 5
Keywords: asynchronous programming, async/await, async await, task asynchronous programming, tap c#, async await c#, async await .net 5, async await .net core, asynchronous programming .net core, asynchronous programming .net 5, asynchronous programming c#, async/await .net 5, async/await .net core, async/await c#
Id: isFZtqnkFZE
Channel Id: undefined
Length: 18min 13sec (1093 seconds)
Published: Sun Dec 20 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.