what is the use of async and await keywords in C#
Suppose we are using two methods as Method1 and Method2 respectively, and both the methods are not dependent on each other, and Method1 takes a long time to complete its task. In Synchronous programming, it will execute the first Method1 and it will wait for the completion of the first method, and then it will execute Method2. Thus, it will be a time-intensive process even though both methods are not depending on each other.
We can run all the methods parallelly by using simple thread programming, but it will block UI and wait to complete all the tasks. To come out of this problem, we have to write too many codes in traditional programming, but if we use the async and await keywords, we will get the solutions in much less code.
if Method3 has a dependency of method1, then it will wait for the completion of Method1 with the help of await keyword.
Async and await in C# are the code markers, which marks code positions from where the control should resume after a task completes.
We can excute all the methods parallelly it will not effect one on the other.
2.async and await keywords are used to create asynchronous methods. The async keyword specifies that a method is an asynchronous method and the await keyword specifies a suspension point. The await operator signalls that the async method can't continue past that point until the awaited asynchronous process is complete. In the meantime, control returns to the caller of the async method.
3.Asynchronous is all about making your application main UI threa or main thread non blocking Which means main working thread will be non block.but not about creating more thread and improving performance. In real time Some method will take more time to execute so it will block the main thread and any code after the method will not excute .To make this method in C# we will make this method Async. When you make a method to asynch It will continue excuting , The main thred will not wait for to finsh the method.Asyn is not creating new thread it will use the main thread.
0 Comments