What are Async and Await ( .NET 4.5 Interview question with answers)?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
In this video we will try to understand What exactly are Async and Await keywords in .NET Both Asynch and Await keywords are new features which have been introduced in .NET 4.5 Framework Let me give a one liner description of what Asynch and Await commands are Aynch and Await are nothing but they are Markers They are markers which mark code positions from where the control should resume after a task or a thread completes Let me repeat what I have said Asynch and Await are nothing but they are code markers Which mark code positions from where control should resume after a task or a thread completes Let me make you understand this description what I have said in a more better manner or even more practical manner I have a very simple code here which is nothing but its a very simple console application This console application what it does is It has the static void main The static void main invokes a method and this method actually invokes a long task method In a Asynchronous mode or I say rather in a Parallel mode This long task method actually waits for 20 seconds I have put a Thread.Sleep here This Thread.Sleep actually happens for 20 seconds It is 20 Thousand Milli Seconds Which is equivalent to 20 seconds What is a problem with this code? I am going to put a debug point here run this application in a debug mode Let me start this debugging Let me press F11 From static void main method is invoked here From this line I am invoking this long task in a multi threaded way or I'll say in a parallel way I am using this task parallel library here In case you are new to task parallel library What my would be suggestion to go and see the Threading Q & A video series From questpondvd.com where I have explained what is TPL What advantages TPL have and how it is different from Threading I will refrain from talking about TPL here In case you are interested please go and see Those videos which are like 30-40 Minutes of videos I am sure you will understand TPL in a better manner In this video we will just restrict ourselves to Aynch and Await keywords From this line from this code I will say If I do a F11 you can see the debug point just gone ahead to this console.writeline new thread This long task is invoked in a multi threaded way The line just went ahead or I'll say the code just went ahead and he started executing console.writeline new thread What I want here is I want that this code or this line should run after this 20 seconds has finished executing I want that at this position this code should actually wait until this long task finishes How do we go about achieving that One way is you write lengthy codes Thread.wait then say Thread.sleep Then you again write those synchronization logic between method and long task That is one way of doing it Or the other way is use the new easy .NET 4.5 feature Asynch and a Await You can say that The control should wait here I am putting a code marker here As I have said Asynch and Await are nothing but they are code markers I am saying here wait at this line until this long task finishes wherever you write this Await keyword You need to ensure that Method is marked by the async keyword Asynch and Await are keywords which we have to use in par You cannot use Await without Asynch You cannot use Aynch without Await At this point please wait here until this long task finishes Because I have used a Await keyword inside this method I need to mark this method as Asynch If I now go ahead and if I say debug Let me say F11 here You can see from this point onwards This code of line will go in a wait mode If I do a F11 if I put a debug point to the next line of code here Even if I put a debug point here even if I do F11 This line of has gone in a wait mode Until this long task finishes this 20 second time This console.writeline new thread will not be executed Once the 20 second time finishes then this line of code will execute After 20 seconds there it is Now the console.writeline is now getting execute In other words Asynch and Await are nothing but they are code markers which says that from which position onwards the code should continue after you invoke a method or a function in a multi threaded or in a parallel way If I do a F11 there it is You can see the Main thread went ahead He was released But the new thread This console.writetline actually got executed after the long task finished executing The main thread was released I wanted to make sure that everyone is getting the point The Main thread did not wait The Main thread went ahead and executed console.writeline main thread This method went in a halt mode until this await finishes That was a very simple demonstration What exactly is the use of Asynch and Await command in .Net 4.5 I hope you enjoyed this video In this video we were trying to understand what is a use of Asynch and Await commands in .NET Thank You So much
Info
Channel: .NET Interview Preparation videos
Views: 246,599
Rating: undefined out of 5
Keywords: .NET 4.5, Async, Await, .NET 4.5 interview questions with answers, Learn .NET 4.5, Step By Step .NET 4.5, async await task in c#, c# wait for asynctask to finish, async vs await, task async and await in c#, c# async and await tutorial, c# async await task tutorial, c# task example await, await c#, async c#, c# interview questions with answers, csharp interview questions, c-sharp interview questions, difference between async and await in c#, async vs await c# interview questions
Id: V2sMXJnDEjM
Channel Id: undefined
Length: 6min 19sec (379 seconds)
Published: Mon Sep 23 2013
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.