

We can use all of these in the real-time project with the help of async and await keyword for the faster execution of the task.
#AASYNC XMLREADER WINDOWS#
NET Framework 4.5 and the Windows runtime contains methods that support async programming.

There are some supporting API’s from the. Here, await keyword is playing a vital role for waiting of Method1 task completion.
#AASYNC XMLREADER CODE#
In the code given above, Method3 requires one parameter, which is the return type of Method1. We are going to create a new method as callMethod and in this method, we are going to call our all Methods as Method1, Method2, and Method3 respectively.Ĭonsole.WriteLine("Total count is " + count) We can not use await keyword without async and we cannot use the async keyword in the Main method for the console Application because it will give the error given below. Here, we have to use the await keyword before passing a parameter in Method3 and for it, we have to use the async keyword from the calling method. In this example, Method1 is returning total length as an integer value and we are passing a parameter as a length in a Method3, which is coming from Method1. Now, coming to the second example, suppose we have Method3, which is dependent on Method1 Here, we can clearly see Method1 and Method2 are not waiting for each other. In the code given above, Method1 and Method2 are not dependent on each other and we are calling from the Main method. In this example, we are going to take two methods, which are not dependent on each other.

We are going to take a console Application for our demonstration. Sample examples of async and await keyword in C# Let’s start with practical examples for understanding the programming concept. To come out of this problem, we have to write too many codes in traditional programming but if we will simply use the async and await keywords, then we will get the solutions in much less code.Īlso, we are going to see more examples, if any third Method as Method3 has a dependency of method1, then it will wait for the completion of Method1 with the help of await keyword.Īsync and await are the code markers, which marks code positions from where the control should resume after a task completes. We can run all the methods parallelly by using the simple thread programming but it will block UI and wait to complete all the tasks. Thus, it will be a time intensive process even though both the methods are not depending on each other. In Synchronous programming, it will execute the first Method1 and it will wait for completion of this method and then it will execute Method2. Suppose, we are using two methods as Method1 and Method2 respectively and both the methods are not dependent on each other and Method1 is taking a long time to complete its task. We will get all the benefits of traditional Asynchronous programming with much less effort by the help of async and await keywords. By using Asynchronous programming, the Application can continue with the other work that does not depend on the completion of the whole task. In other words, if any process is blocked in a synchronous application, the entire application gets blocked and our application stops responding until the whole task completes.Īsynchronous programming is very helpful in this condition. When we are dealing with UI and on button click, we use a long-running method like reading a large file or something else which will take a long time, in that case, the entire application must wait to complete the whole task. Nowadays, Asynchronous programming is very popular with the help of the async and await keywords in C#.
