SlideShare a Scribd company logo
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?

More Related Content

PPTX
C# Async Await
PPTX
Asynchronous Programming in .NET
PPT
Asynchronous Programming in C# - Part 1
PPTX
Asynchronous programming in C#
PPTX
Async Programming in C# 5
PPTX
Learn Javascript Basics
PPTX
Message Broker System and RabbitMQ
PPTX
Apache Ambari: Managing Hadoop and YARN
C# Async Await
Asynchronous Programming in .NET
Asynchronous Programming in C# - Part 1
Asynchronous programming in C#
Async Programming in C# 5
Learn Javascript Basics
Message Broker System and RabbitMQ
Apache Ambari: Managing Hadoop and YARN

What's hot (19)

PDF
Consumer offset management in Kafka
PDF
Spring core module
PDF
Sync, async and multithreading
PPTX
Intoduction of FIrebase Realtime Database
PPTX
Memory management
PPTX
ASP.NET Lecture 1
PPTX
Kafka PPT.pptx
PPTX
React vs Angular
PPTX
Angular 5 presentation for beginners
PPTX
Angular tutorial
ODP
Introduction To RabbitMQ
PDF
Building Stream Infrastructure across Multiple Data Centers with Apache Kafka
PDF
From AWS Data Pipeline to Airflow - managing data pipelines in Nielsen Market...
PPTX
MVC, MVP e MVVM: Uma Comparação de Padrões Arquiteturais
PPTX
Overloading of io stream operators
PPTX
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
PDF
Introducing Async/Await
PDF
Performance testing of mobile apps
PDF
Cassandra Summit 2014: Active-Active Cassandra Behind the Scenes
Consumer offset management in Kafka
Spring core module
Sync, async and multithreading
Intoduction of FIrebase Realtime Database
Memory management
ASP.NET Lecture 1
Kafka PPT.pptx
React vs Angular
Angular 5 presentation for beginners
Angular tutorial
Introduction To RabbitMQ
Building Stream Infrastructure across Multiple Data Centers with Apache Kafka
From AWS Data Pipeline to Airflow - managing data pipelines in Nielsen Market...
MVC, MVP e MVVM: Uma Comparação de Padrões Arquiteturais
Overloading of io stream operators
Reactive programming by spring webflux - DN Scrum Breakfast - Nov 2018
Introducing Async/Await
Performance testing of mobile apps
Cassandra Summit 2014: Active-Active Cassandra Behind the Scenes
Ad

Similar to Async programming in c# (20)

DOCX
Asynchronyin net
PPTX
C# 5 deep drive into asynchronous programming
PPTX
Asynchronous programming - .NET Way
PPTX
Concurrency in c#
PDF
Why async matters
PPTX
History of asynchronous in .NET
PPTX
C# Async/Await Explained
PDF
Async Await for Mobile Apps
PDF
PPTX
Synchronous vs Asynchronous Programming
PPTX
Training – Going Async
PPSX
Async-await best practices in 10 minutes
PPTX
End to-end async and await
PPTX
Async and Await on the Server
PPTX
Async in .NET
PDF
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
KEY
Asynchronous Programming
PPTX
Async/Await
PPTX
MobConf - session on C# async-await on 18june2016 at Kochi
PDF
Asynchronous Programming in C# and .NET Core - Edukite
Asynchronyin net
C# 5 deep drive into asynchronous programming
Asynchronous programming - .NET Way
Concurrency in c#
Why async matters
History of asynchronous in .NET
C# Async/Await Explained
Async Await for Mobile Apps
Synchronous vs Asynchronous Programming
Training – Going Async
Async-await best practices in 10 minutes
End to-end async and await
Async and Await on the Server
Async in .NET
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
Asynchronous Programming
Async/Await
MobConf - session on C# async-await on 18june2016 at Kochi
Asynchronous Programming in C# and .NET Core - Edukite
Ad

More from Ahasanul Kalam Akib (6)

PPTX
Dependency injection presentation
PPTX
Getting Started with MongoDB
PPTX
31 days Refactoring
PPTX
3D printing in medical science
PPTX
Water Level Detector With Alarm System
Dependency injection presentation
Getting Started with MongoDB
31 days Refactoring
3D printing in medical science
Water Level Detector With Alarm System

Recently uploaded (20)

PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Revamp in MTO Odoo 18 Inventory - Odoo Slides
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PPTX
Onica Farming 24rsclub profitable farm business
PDF
From loneliness to social connection charting
PPTX
Introduction and Scope of Bichemistry.pptx
PPTX
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Business Ethics Teaching Materials for college
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Open folder Downloads.pdf yes yes ges yes
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Revamp in MTO Odoo 18 Inventory - Odoo Slides
STATICS OF THE RIGID BODIES Hibbelers.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Nursing Management of Patients with Disorders of Ear, Nose, and Throat (ENT) ...
Open Quiz Monsoon Mind Game Final Set.pptx
The Final Stretch: How to Release a Game and Not Die in the Process.
Onica Farming 24rsclub profitable farm business
From loneliness to social connection charting
Introduction and Scope of Bichemistry.pptx
COMPUTERS AS DATA ANALYSIS IN PRECLINICAL DEVELOPMENT.pptx
Abdominal Access Techniques with Prof. Dr. R K Mishra
Module 3: Health Systems Tutorial Slides S2 2025
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Business Ethics Teaching Materials for college
Renaissance Architecture: A Journey from Faith to Humanism
NOI Hackathon - Summer Edition - GreenThumber.pptx
102 student loan defaulters named and shamed – Is someone you know on the list?
Open folder Downloads.pdf yes yes ges yes
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx

Async programming in c#

  • 1. Asynchronous programming in C# Presented By: Ahasanul Kalam Akib - Software Engineer Imtiyaz Hossain - Software Engineer
  • 2. 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. 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. 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. 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.
  • 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 Single threaded environment Multi threaded environment
  • 10. 10 Asynchronous programming model Single threaded environment Multi threaded environment
  • 11. 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. 12 Async await in practice Blocking Code • GetAwaiter().GetResult() • Result • Wait Non-Blocking Code • await
  • 13. 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. 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.