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
PPTX
Asynchronous programming
PPTX
Asynchronous programming in C#
PDF
Cypress - Best Practices
PDF
Test Automation
PDF
Selenium webdriver interview questions and answers
PPT
Reflection in java
C# Async Await
Asynchronous Programming in .NET
Asynchronous programming
Asynchronous programming in C#
Cypress - Best Practices
Test Automation
Selenium webdriver interview questions and answers
Reflection in java

What's hot (20)

PPTX
Python in Test automation
PPTX
Async Programming in C# 5
PPTX
ASP.NET Core
PDF
Learn Java with Dr. Rifat Shahriyar
PPTX
Multithreading in java
PPTX
Smart pointers
PPTX
Introduction to Kotlin
PDF
Introduction to Kotlin coroutines
PPTX
Robot Framework
PPTX
Software architecture for high traffic website
PPT
9. Input Output in java
PDF
Test Automation Using Python | Edureka
PPTX
Selenium test automation
PPTX
Abstraction java
PPTX
Cypress Testing.pptx
PPT
CPP Language Basics - Reference
PPT
Introduction to the Web API
PDF
Afinal, o que são Single Page Applications
PDF
Introduction cypress
Python in Test automation
Async Programming in C# 5
ASP.NET Core
Learn Java with Dr. Rifat Shahriyar
Multithreading in java
Smart pointers
Introduction to Kotlin
Introduction to Kotlin coroutines
Robot Framework
Software architecture for high traffic website
9. Input Output in java
Test Automation Using Python | Edureka
Selenium test automation
Abstraction java
Cypress Testing.pptx
CPP Language Basics - Reference
Introduction to the Web API
Afinal, o que são Single Page Applications
Introduction cypress
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
PPTX
Synchronous vs Asynchronous Programming
PDF
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
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Institutional Correction lecture only . . .
PDF
Complications of Minimal Access Surgery at WLH
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
01-Introduction-to-Information-Management.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Business Ethics Teaching Materials for college
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
master seminar digital applications in india
PPTX
Cell Structure & Organelles in detailed.
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Institutional Correction lecture only . . .
Complications of Minimal Access Surgery at WLH
Microbial disease of the cardiovascular and lymphatic systems
VCE English Exam - Section C Student Revision Booklet
01-Introduction-to-Information-Management.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Business Ethics Teaching Materials for college
O7-L3 Supply Chain Operations - ICLT Program
2.FourierTransform-ShortQuestionswithAnswers.pdf
master seminar digital applications in india
Cell Structure & Organelles in detailed.
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pharma ospi slides which help in ospi learning
Week 4 Term 3 Study Techniques revisited.pptx
human mycosis Human fungal infections are called human mycosis..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.