Asynchronous programming in C# Presented By: Ahasanul Kalam Akib - Software Engineer Imtiyaz Hossain - Software Engineer
2 What is asynchronous programming? • Asynchronous means that the current thread is freed while you are waiting for a response to some I/O operation. (local storage, a network request, etc.) • A sort of parallel programming that permits a unit of labor to run separately from the first application thread. • Is a key technique that makes it straight forward to handle blocking I/O and concurrent operations on multiple cores. • Asynchronous code isn't about multi-threading. Actually the opposite: Part of the benefit of asynchronous code is to not need more threads.
3 What is asynchronous programming? There are two types of work that are done asynchronously: I/O-bound operations could be • File-system accesses • HTTP requests, • API calls, or database queries. CPU-bound operations would be • like encrypting data • complex calculations • image or document management. Which are: • Heavy computational work • Needs continuous CPU involvement Hence, This will need a dedicated thread, and will generally use a ThreadPool thread • Which can be done without CPU system. • May not need any dedicated threads • The network or disk driver may handle it by themselves
4 Benefits? • Keep the UI of our app responsive. • Can improve the performance of our application. • Utilization of multi-core systems • Avoid thread pool starvation
5 Synchronous vs Asynchronous Asynchronous In asynchronous operations, on the other hand, you can move to another task before the previous one finishes. This way, with asynchronous programming you’re able to deal with multiple requests simultaneously, Synchronous In synchronous operations tasks are performed one at a time and only when one is completed, the following is unblocked. In other words, you need to wait for a task to finish to move to the next one.
6 Synchronous vs Asynchronous Synchronous Asynchronous
7 Asynchronous programming pattern • Task based async pattern (Recommended) • Event based async pattern (Legacy) • Async programming model (Legacy) Normal Read method TAP EAP APM
8 Multi-threading vs Asynchronous Environments • Single threaded • Multi-threaded Programming Model • Synchronous • Asynchronous
9 Synchronous programming model Single threaded environment Multi threaded environment
10 Asynchronous programming model Single threaded environment Multi threaded environment
11 Async await in practice Syntax: public async void/Task/Task<T> MethodAsync(param1, param2) { doSynchronousTask(); await doAsyncTask(); } • The async enables the await functionality in the method • You can not use await without using the async • A method can be declared as async without using await in the method body. It does work, but the just runs synchronously • Three types of return type void/Task/Task<T>
12 Async await in practice Blocking Code • GetAwaiter().GetResult() • Result • Wait Non-Blocking Code • await
13 How is it useful? For example, consider a web API call that reads data from a database. What happens when 1000 requests come in at the same time? Synchronous Way: • Need a separate thread for each request • ASP.NET has a maximum thread count (ex. 3000) • Max count will be reached very soon • Have to wait until some of the first requests are completed before they can even start Asynchronous Way: • As soon as the database request is made, the thread is freed while it waits for a response from the database. • During that waiting time, ASP.NET can use that thread to start processing a new request. • The result is that you need less threads to do the same amount of work • Increase the scalability
14 Drawbacks • Code gets more complex and harder to maintain. • There is increased memory allocation, as some objects have to stay alive longer while awaiting other code to be executed. • It can get hard to find bugs occurring in asynchronous tasks. • When we're writing an asynchronous piece of code, all our application code tends to become asynchronous.
15 References • https://docs.microsoft.com/en-us/dotnet/standard/async-in-depth • https://www.pluralsight.com/guides/understand-control-flow-async-await
Thank You! Q&A?

Async programming in c#

  • 1.
    Asynchronous programming in C# PresentedBy: Ahasanul Kalam Akib - Software Engineer Imtiyaz Hossain - Software Engineer
  • 2.
    2 What is asynchronousprogramming? • Asynchronous means that the current thread is freed while you are waiting for a response to some I/O operation. (local storage, a network request, etc.) • A sort of parallel programming that permits a unit of labor to run separately from the first application thread. • Is a key technique that makes it straight forward to handle blocking I/O and concurrent operations on multiple cores. • Asynchronous code isn't about multi-threading. Actually the opposite: Part of the benefit of asynchronous code is to not need more threads.
  • 3.
    3 What is asynchronousprogramming? There are two types of work that are done asynchronously: I/O-bound operations could be • File-system accesses • HTTP requests, • API calls, or database queries. CPU-bound operations would be • like encrypting data • complex calculations • image or document management. Which are: • Heavy computational work • Needs continuous CPU involvement Hence, This will need a dedicated thread, and will generally use a ThreadPool thread • Which can be done without CPU system. • May not need any dedicated threads • The network or disk driver may handle it by themselves
  • 4.
    4 Benefits? • Keep theUI of our app responsive. • Can improve the performance of our application. • Utilization of multi-core systems • Avoid thread pool starvation
  • 5.
    5 Synchronous vs Asynchronous Asynchronous Inasynchronous operations, on the other hand, you can move to another task before the previous one finishes. This way, with asynchronous programming you’re able to deal with multiple requests simultaneously, Synchronous In synchronous operations tasks are performed one at a time and only when one is completed, the following is unblocked. In other words, you need to wait for a task to finish to move to the next one.
  • 6.
  • 7.
    7 Asynchronous programming pattern •Task based async pattern (Recommended) • Event based async pattern (Legacy) • Async programming model (Legacy) Normal Read method TAP EAP APM
  • 8.
    8 Multi-threading vs Asynchronous Environments •Single threaded • Multi-threaded Programming Model • Synchronous • Asynchronous
  • 9.
    9 Synchronous programming model Singlethreaded environment Multi threaded environment
  • 10.
    10 Asynchronous programming model Singlethreaded environment Multi threaded environment
  • 11.
    11 Async await inpractice Syntax: public async void/Task/Task<T> MethodAsync(param1, param2) { doSynchronousTask(); await doAsyncTask(); } • The async enables the await functionality in the method • You can not use await without using the async • A method can be declared as async without using await in the method body. It does work, but the just runs synchronously • Three types of return type void/Task/Task<T>
  • 12.
    12 Async await inpractice Blocking Code • GetAwaiter().GetResult() • Result • Wait Non-Blocking Code • await
  • 13.
    13 How is ituseful? For example, consider a web API call that reads data from a database. What happens when 1000 requests come in at the same time? Synchronous Way: • Need a separate thread for each request • ASP.NET has a maximum thread count (ex. 3000) • Max count will be reached very soon • Have to wait until some of the first requests are completed before they can even start Asynchronous Way: • As soon as the database request is made, the thread is freed while it waits for a response from the database. • During that waiting time, ASP.NET can use that thread to start processing a new request. • The result is that you need less threads to do the same amount of work • Increase the scalability
  • 14.
    14 Drawbacks • Code getsmore complex and harder to maintain. • There is increased memory allocation, as some objects have to stay alive longer while awaiting other code to be executed. • It can get hard to find bugs occurring in asynchronous tasks. • When we're writing an asynchronous piece of code, all our application code tends to become asynchronous.
  • 15.
  • 16.