Understanding Async and Await

 To understand Async and Await, first we need to understand about 'Task'.

 Task - This is used create thread but it will create thread from the thread pool and not from the computer operating system unlike normal Thread object in c# does. This is used to perform async operations

1. Task<TResult> - This return is used when the async method that returns a value.

2. Task - This return type is used when the async method does not return any value.

3. void - This return type is used for an event handler.


  1. We saw that concurrency refers to, in one way or another, doing several things at the same time. That concept of concurrency encompasses parallel programming and asynchronous programming.
  2. Parallel programming refers to the use of multiple threads simultaneously to solve a set of tasks. For this, we need processors with adequate abilities to perform several tasks at the same time. In general, we use parallel programming to gain speed.
  3. Asynchronous programming refers to the efficient use of threads where we do not block a thread unnecessarily. But while we wait for the result of an operation, the thread gets to perform other tasks in the meantime. This increases vertical scalability and allows us to prevent the user interface from freezing during long tasks.

    
    caller method - parent method
    calling method - child method

    Ex: Main method is caller method. Inside main, the methods are calling method.

    For every method marked as 'async' then there should be 'await' keyword inside of that method.
    
Below code await keyword, if the there is some code to run then that code will be executed by different thread from threadpool and this is maintained by synchronization context But main thread will not execute that code.

after await line of code is executed or reached then the control of the project will go back to its Main parent caller and not to its previous parent caller [Here very important point to note is our .NetFramework runtime CLR will keep track of all await markers. Once the flow is in Main thread then CLR runtime knows from where the flow should start]. This makes sense becuase we want to make UI thread or Main thread to free. If its go back to its previous parent caller then Main thread or UI thread will never get free and the UI will be freezed and user cannnot do anything on it.

after await line of code is executed or reached then the control of the project will go back to its Main parent caller and not to its previous parent caller. Please once the last await is done its work or completed then after executing await below line of code, the control will jump back to its previous parent caller and not main thread caller

    

Remember that once await keyword is detected 

1 : It will go inside await function and then only it will jump back to Main thread
2 : Once the code is in Main thread, if there is some lines to execute, it will start executing. At the same time, await block of code gets executed by different thread through the help context switch method. **** KEEP IN MIND THAT "await block will execute only if main thread is active". If in case main thread is exited by some unknown reason then await will stop executing.
3 : We should always call the custom methods or user defined methods inside Task.Run method. If there is already an .NET Asynchronous method like for example "DownloadStringTaskAsync" then we can directly call using await keyword.

https://www.bytehide.com/blog/async-await-csharp


Comments

Popular posts from this blog

Understanding Collection and Object Initializer

How to execute Javascript in VS code