Async and await in C# example

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
this is part one zero one of C sharp tutorial in this video we'll discuss the use of async and evade keywords with an example so here is what we want to do we have a notepad here which has some data let us create a simple windows forms application that counts the number of characters that we have in this notepad let us assume the file is very big and it takes around five seconds to read and count the number of characters in the file when the process file button is clicked we want the application to display processing file please wait message so the user knows the application is busy processing the file as soon as the application finishes processing the file it should display the number of characters as you can see here another important requirement is that the application should remain responsive throughout the entire process that is when the application is busy processing the file the application should not hang and we should still be able to interact with the application we should be able to click within the other controls on the form move the form around on the screen resize it if required etc first let's create the application without using the async innovate keywords and see how it behaves what I have done so far is created this notepad with name data dot txt within this notepad we have caught some text we want to read the number of characters that we have here this data dot txt notepad is present in the data folder which in turn is present in C Drive I've also created a new windows forms application to create a new windows forms application click on file new project and within the new project dialog box select we should see sharp and you'll see VIN as forms application I have named this windows forms application async example and on this windows form I have a button and I have set a few properties on this button first of all I have set the name of the button to be TN process file and the font size to 10 and then finally the text on the button to process file this forum we also have a label and here is that label for the label I have set name to LVL count this is the label which will be displaying this message processing file please wait and then once it has finished processing the file the number of characters that we have in the file so I have set the name of the label to LVL count and again font size to 10 and finally text to an empty string and for the form itself I have set text property to count characters in a file now let's go ahead and generate click event handler for the button so select the button and then within the properties window click on the events icon and then click on the click event here which is going to generate the click event handler method now let's right click on the form and view code so here we have the click event handler so the first thing that you want to do here is create a method which is going to process the file and count the number of characters with a notepad so this is going to be a private method it's going to return us an integer that is the number of characters that we have in the file and let's name the method count characters this method is not going to take any parameters the first thing that we are going to do here is create a variable count and initialize it to 0 the next thing that we want to do is create an instance of streamreader which is going to read the file from the disk and the stream reader Clause is present in system dot IO namespace so let's bring that namespace in and now let's create an instance of stream reader let's call it reader equals new stream reader and we want the stream reader to be pointing to our notepad so let's specify the path for our notepad our notepad is present in C Drive within data folder and the name of the notepad is dot txt and then now let's create a string variable let's call it content equals reader dot read to end so this method is going to read all the contents of the notepad and we are going to store that content in this variable and to get the number of characters all we have to do is on the string variable use the length property which is going to give us the number of characters and let's do that within our count variable now our notepad is not that big so this application is going to take a few milliseconds to process that to make the application look busy for five seconds let's make the thread that is processing to sleep for five seconds so first let's bring in system dot threading namespace and then let's make the current thread sleep for five thousand milliseconds which is five seconds and then finally let's return whatever we have in our count variable so this is a the private method which is going to count the number of characters that we have in our notepad now we want to call this method with another click even Handler so here let's create a variable I'm going to call it count let's initialize that to zero and the first thing that we want to do here is within our label let's display the text which says processing file please wait and then you know let's call over count characters function which is going to process the file and remember this is going to be busy for at least five seconds and then as soon as it finishes executing this method within this variable we will have the number of characters that we have in the notepad which we want to display in the label so LBL count dot txt equals count dot to string and to this lets append characters in file all right so let's go ahead and run our application by pressing ctrl f5 we have our application running here when we click this process file button we are going to have two problems first of all it will not display the status message processing file please wait and the application also becomes unresponsive while it's busy processing the file will not be able to move the windows form around like this and we also will not be able to resize the form while it is busy processing the file let's actually look at that in action let's click the process file button notice we don't see the status message I am NOT able to move it around and I am not able to resize it as well once it has finished processing now I am able to move it around and we can resize it as well and now we see the number of characters that we have in the file and here is that blocking example code that we just discussed now let's see how to make this windows forms application responsive by using the async and evade keywords the only method that needs to change is this BTN process file click method let's make this method async by using the keyword async so basically this is telling we can call this method asynchronously and then within the method we are going to create a task you can think of tasks as a unit of to do this task class is present in a different namespace and that is systems or threading dot tasks so make sure you have that using declaration now we are going to use this task to call this count characters method this is a method which is going to take a bit of time to execute so we are offloading that responsibility to this task now since this count characters is returning an integer we are actually going to create tasks of integer let's call the instance task equals new task and we want this task to execute this count characters method so let's pass the name of the method here and then let's start the task so this task now is going to execute this count characters function and while the task is busy executing the count characters function the UI is free and the user will be able to interact with the form you know the form will no longer be blocking we'll look at that in action in just a bit so we have our task which is executing our long-running job here which is count characters and then another change that we need to do is right here so when the application has reached at this point you know we have to wait for this task to complete right so to signal that we are going to use evade keyword and we are going to await on the task so right here the application will wait for the task to complete processing count characters and then return and at that point we'll have the number of characters in the notepad which they UI will then display in the label so three simple steps here to use our async and await keywords and make this application responsive let's go ahead and run our application by pressing ctrl f5 we have our application running and when we click this process file button notice the status message processing file please wait is immediately displayed and the application is also Responsive look at this when the application is busy processing the file I can move it around and I can also resize it so here we have a non-blocking responsive application we have of a non-blocking example code that we just discussed right here so what is the use of async and await keywords in c-sharp async and await keywords i used to create asynchronous methods the async keyword specifies that a method is an asynchronous method and the evade keyword specifies a suspension point the evade operator signals that the async method can't continue past that point until the awaited process is complete in the mean time control returns to the color of the async method an async method typically contains one or more occurrences of an await operator but the absence of our ate expression doesn't cause a compiler error if you have any experience writing multi-threaded programs then you might get a few questions at this point can we achieve this using threads what is the difference between a thread and a task and when to use one over the other we'll answer all these questions in our next video thank you for listening and have a great day
Info
Channel: kudvenkat
Views: 488,537
Rating: undefined out of 5
Keywords: c#, async, await, task, example, tutorial, winforms async button click, task async and await in c#, c# async await tutorial, winforms async await example, c# winforms async await, winforms async event handler, c# task update ui, c# task blocks ui thread, c# wait for asynctask to finish, c# task example await, c# wait for task to finish before continuing, c# async await update ui, c# async await task tutorial, asp.net async and await, c# async await ui thread
Id: C5VhaxQWcpE
Channel Id: undefined
Length: 11min 56sec (716 seconds)
Published: Mon Oct 31 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.