SlideShare a Scribd company logo
5
Most read
7
Most read
8
Most read
Asynchronous
programming in C#
BOHDAN PASHKOVSKYI
CORECAMP Ivano-Frankivsk 2017
Multi-threading vs Asynchronous
Here, there are two completely different concepts involved, First –
Synchronous and Asynchronous programming model and second –
Single threaded and multi-threaded environments. Each
programming model (Synchronous and Asynchronous) can run in
single threaded and multi-threaded environment.
CORECAMP Ivano-Frankivsk 2017
CORECAMP Ivano-Frankivsk 2017
CORECAMP Ivano-Frankivsk 2017
Synchronous Programming model – In this programming model, A thread is assigned to one task and starts
working on it. Once the task completes then it is available for the next task. In this model, it cannot leave the
executing task in mid to take up another task. Let’s discuss how this model works in single and multi-threaded
environments.
Single Threaded – If we have couple of tasks to be worked on and the current system provides just a single thread,
then tasks are assigned to the thread one by one. It can be pictorially depicted as
CORECAMP Ivano-Frankivsk 2017
Multi-Threaded – In this environment, we used to have multiple threads which can take up these tasks and start
working on that. It means we have a pool of threads (new threads can also be created based on the requirement
and available resources) and bunch of tasks. So these thread can work on these as
CORECAMP Ivano-Frankivsk 2017
Asynchronous Programming Model – In contrary to Synchronous programming model, here a thread once start executing a
task it can hold it in mid, save the current state and start executing another task.
CORECAMP Ivano-Frankivsk 2017
If our system is capable of having multiple threads then all the threads can work in asynchronous model as well
CORECAMP Ivano-Frankivsk 2017
So till now we have discussed four scenarios –
1. Synchronous Single Threaded
2. Synchronous Multi-Threaded
3. Asynchronous Single Threaded
4. Asynchronous Multi-Threaded
CORECAMP Ivano-Frankivsk 2017
private void StartButtonClick(object sender, RoutedEventArgs e)
{
string text = Download();
TextBox.Text = text;
}
private string Download()
{
return new WebClient().DownloadString("http://corecamp.net/");
}
Synchronous method in Desktop application
CORECAMP Ivano-Frankivsk 2017
private async void StartButtonClick(object sender, RoutedEventArgs e)
{
string text = await Download();
TextBox.Text = text;
}
private async Task<string> Download()
{
return await new WebClient().DownloadStringTaskAsync("http://corecamp.net/");
}
Asynchronous method in Desktop application
CORECAMP Ivano-Frankivsk 2017
Evolution of Asynchronous concepts
• Asynchronous Programming Model (APM)
• Evented Asynchronous Programming (EAP)
• Task-based Asynchronous Programming (TAP)
CORECAMP Ivano-Frankivsk 2017
Asynchronous Programming Model (APM)
// .NET 1 model
file.BeginRead(buffer, 0, maxLength, asyncResult => {
int numBytesRead = files.EndRead(asyncResult);
// Now do something with "buffer"
}, null);
CORECAMP Ivano-Frankivsk 2017
Evented Asynchronous Programming (EAP)
// .NET 2 model
webClient.DownloadStringCompleted += (sender, args) => {
string html = args.Result;
// Now do someting with "html"
};
webClient.DownloadStringAsync(new Uri("http://example.com"));
CORECAMP Ivano-Frankivsk 2017
Task-based Asynchronous Programming (TAP)
Task<string> htmlTask = webClient.DownloadStringTaskAsync(url);
1. string html = htmlTask.Result; // Sync (block until done)
2. htmlTask.ContinueWith(task => {
string html = task.Result; // Async, C# 4
});
3. string html = await htmlTask; // Async. C# 5
CORECAMP Ivano-Frankivsk 2017
DEMO
Thank you for the
attention!

More Related Content

What's hot (20)

PPT
C# Exceptions Handling
sharqiyem
 
PDF
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
PPT
Java interfaces
Raja Sekhar
 
PPT
Java And Multithreading
Shraddha
 
PPT
Thread model in java
AmbigaMurugesan
 
PPTX
Session tracking in servlets
vishal choudhary
 
PDF
Introduction to kotlin coroutines
NAVER Engineering
 
PDF
Functional programming
ijcd
 
PDF
JDBC : Java Database Connectivity
DevAdnani
 
PPT
Java Servlets
BG Java EE Course
 
PDF
Oop basic overview
Deborah Akuoko
 
PPT
Object Oriented Concepts and Principles
deonpmeyer
 
PPT
Ch 3 event driven programming
Chaffey College
 
PPTX
Nodejs functions & modules
monikadeshmane
 
PPTX
Abstract class and Interface
Haris Bin Zahid
 
PPTX
Namespaces in C#
yogita kachve
 
PPT
Introduction to java beans
Hitesh Parmar
 
ODP
OOP java
xball977
 
PPTX
Threads in JAVA
Haldia Institute of Technology
 
PPTX
Angular interview questions
Goa App
 
C# Exceptions Handling
sharqiyem
 
Oops concepts || Object Oriented Programming Concepts in Java
Madishetty Prathibha
 
Java interfaces
Raja Sekhar
 
Java And Multithreading
Shraddha
 
Thread model in java
AmbigaMurugesan
 
Session tracking in servlets
vishal choudhary
 
Introduction to kotlin coroutines
NAVER Engineering
 
Functional programming
ijcd
 
JDBC : Java Database Connectivity
DevAdnani
 
Java Servlets
BG Java EE Course
 
Oop basic overview
Deborah Akuoko
 
Object Oriented Concepts and Principles
deonpmeyer
 
Ch 3 event driven programming
Chaffey College
 
Nodejs functions & modules
monikadeshmane
 
Abstract class and Interface
Haris Bin Zahid
 
Namespaces in C#
yogita kachve
 
Introduction to java beans
Hitesh Parmar
 
OOP java
xball977
 
Angular interview questions
Goa App
 

Similar to Asynchronous programming in C# (20)

PPTX
Asynchronous Programming.pptx
Aayush Chimaniya
 
PPT
React native
Mohammed El Rafie Tarabay
 
PPTX
AsynchronousProgrammingDesignPatterns.pptx
Abhishek Sagar
 
PDF
Asynchronous API in Java8, how to use CompletableFuture
José Paumard
 
PDF
Minko - Targeting Flash/Stage3D with C++ and GLSL
Minko3D
 
PDF
Apache Cassandra and Apche Spark
Alex Thompson
 
PPTX
Designing a machine learning algorithm for Apache Spark
Marco Gaido
 
PDF
Beginning MEAN Stack
Rob Davarnia
 
PPTX
Js engine performance
paullfc
 
PDF
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
Somenath Mukhopadhyay
 
PPTX
NodeJS guide for beginners
Enoch Joshua
 
PDF
Solvcon PyCon APAC 2014
weijr
 
PDF
(1) c sharp introduction_basics_dot_net
Nico Ludwig
 
PPTX
Java programming(unit 1)
Dr. SURBHI SAROHA
 
PDF
Node js
Rohan Chandane
 
PPTX
Intro to programing with java-lecture 1
Mohamed Essam
 
PDF
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
NETFest
 
PDF
Composer Helpdesk
Sven Rautenberg
 
PPTX
Let's play with ASP.NET 5 (vNext) RC1
Ugo Lattanzi
 
PDF
Power-Efficient Programming Using Qualcomm Multicore Asynchronous Runtime Env...
Qualcomm Developer Network
 
Asynchronous Programming.pptx
Aayush Chimaniya
 
AsynchronousProgrammingDesignPatterns.pptx
Abhishek Sagar
 
Asynchronous API in Java8, how to use CompletableFuture
José Paumard
 
Minko - Targeting Flash/Stage3D with C++ and GLSL
Minko3D
 
Apache Cassandra and Apche Spark
Alex Thompson
 
Designing a machine learning algorithm for Apache Spark
Marco Gaido
 
Beginning MEAN Stack
Rob Davarnia
 
Js engine performance
paullfc
 
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
Somenath Mukhopadhyay
 
NodeJS guide for beginners
Enoch Joshua
 
Solvcon PyCon APAC 2014
weijr
 
(1) c sharp introduction_basics_dot_net
Nico Ludwig
 
Java programming(unit 1)
Dr. SURBHI SAROHA
 
Intro to programing with java-lecture 1
Mohamed Essam
 
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
NETFest
 
Composer Helpdesk
Sven Rautenberg
 
Let's play with ASP.NET 5 (vNext) RC1
Ugo Lattanzi
 
Power-Efficient Programming Using Qualcomm Multicore Asynchronous Runtime Env...
Qualcomm Developer Network
 
Ad

More from Bohdan Pashkovskyi (10)

PDF
Деякі аспекти встановлення дорожніх знаків
Bohdan Pashkovskyi
 
PPTX
Telegram bots
Bohdan Pashkovskyi
 
PPTX
CoreCamp "Automated testing basics for developers"
Bohdan Pashkovskyi
 
PPTX
CQRS and Event Sourcing
Bohdan Pashkovskyi
 
PPTX
C# Lesson 3
Bohdan Pashkovskyi
 
PPTX
C# Lesson 1
Bohdan Pashkovskyi
 
PPTX
Automated testing
Bohdan Pashkovskyi
 
PPTX
IF .NET User Group
Bohdan Pashkovskyi
 
PPTX
ASP .NET MVC - best practices
Bohdan Pashkovskyi
 
PPTX
.Net Core
Bohdan Pashkovskyi
 
Деякі аспекти встановлення дорожніх знаків
Bohdan Pashkovskyi
 
Telegram bots
Bohdan Pashkovskyi
 
CoreCamp "Automated testing basics for developers"
Bohdan Pashkovskyi
 
CQRS and Event Sourcing
Bohdan Pashkovskyi
 
C# Lesson 3
Bohdan Pashkovskyi
 
C# Lesson 1
Bohdan Pashkovskyi
 
Automated testing
Bohdan Pashkovskyi
 
IF .NET User Group
Bohdan Pashkovskyi
 
ASP .NET MVC - best practices
Bohdan Pashkovskyi
 
Ad

Recently uploaded (20)

PDF
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
PPTX
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
PPTX
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
PPT
deep dive data management sharepoint apps.ppt
novaprofk
 
PPTX
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
PDF
Copia de Strategic Roadmap Infographics by Slidesgo.pptx (1).pdf
ssuserd4c6911
 
PDF
Merits and Demerits of DBMS over File System & 3-Tier Architecture in DBMS
MD RIZWAN MOLLA
 
PDF
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PDF
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
PDF
List of all the AI prompt cheat codes.pdf
Avijit Kumar Roy
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PPTX
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PDF
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
PDF
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
PDF
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 
PDF
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
PPTX
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
How to Connect Your On-Premises Site to AWS Using Site-to-Site VPN.pdf
Tamanna
 
apidays Singapore 2025 - Designing for Change, Julie Schiller (Google)
apidays
 
apidays Munich 2025 - Building an AWS Serverless Application with Terraform, ...
apidays
 
deep dive data management sharepoint apps.ppt
novaprofk
 
AI Presentation Tool Pitch Deck Presentation.pptx
ShyamPanthavoor1
 
Copia de Strategic Roadmap Infographics by Slidesgo.pptx (1).pdf
ssuserd4c6911
 
Merits and Demerits of DBMS over File System & 3-Tier Architecture in DBMS
MD RIZWAN MOLLA
 
OPPOTUS - Malaysias on Malaysia 1Q2025.pdf
Oppotus
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
List of all the AI prompt cheat codes.pdf
Avijit Kumar Roy
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
Module-5-Measures-of-Central-Tendency-Grouped-Data-1.pptx
lacsonjhoma0407
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
Web Scraping with Google Gemini 2.0 .pdf
Tamanna
 
WEF_Future_of_Global_Fintech_Second_Edition_2025.pdf
AproximacionAlFuturo
 
R Cookbook - Processing and Manipulating Geological spatial data with R.pdf
OtnielSimopiaref2
 
Product Management in HealthTech (Case Studies from SnappDoctor)
Hamed Shams
 
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 

Asynchronous programming in C#

  • 2. CORECAMP Ivano-Frankivsk 2017 Multi-threading vs Asynchronous Here, there are two completely different concepts involved, First – Synchronous and Asynchronous programming model and second – Single threaded and multi-threaded environments. Each programming model (Synchronous and Asynchronous) can run in single threaded and multi-threaded environment.
  • 5. CORECAMP Ivano-Frankivsk 2017 Synchronous Programming model – In this programming model, A thread is assigned to one task and starts working on it. Once the task completes then it is available for the next task. In this model, it cannot leave the executing task in mid to take up another task. Let’s discuss how this model works in single and multi-threaded environments. Single Threaded – If we have couple of tasks to be worked on and the current system provides just a single thread, then tasks are assigned to the thread one by one. It can be pictorially depicted as
  • 6. CORECAMP Ivano-Frankivsk 2017 Multi-Threaded – In this environment, we used to have multiple threads which can take up these tasks and start working on that. It means we have a pool of threads (new threads can also be created based on the requirement and available resources) and bunch of tasks. So these thread can work on these as
  • 7. CORECAMP Ivano-Frankivsk 2017 Asynchronous Programming Model – In contrary to Synchronous programming model, here a thread once start executing a task it can hold it in mid, save the current state and start executing another task.
  • 8. CORECAMP Ivano-Frankivsk 2017 If our system is capable of having multiple threads then all the threads can work in asynchronous model as well
  • 9. CORECAMP Ivano-Frankivsk 2017 So till now we have discussed four scenarios – 1. Synchronous Single Threaded 2. Synchronous Multi-Threaded 3. Asynchronous Single Threaded 4. Asynchronous Multi-Threaded
  • 10. CORECAMP Ivano-Frankivsk 2017 private void StartButtonClick(object sender, RoutedEventArgs e) { string text = Download(); TextBox.Text = text; } private string Download() { return new WebClient().DownloadString("http://corecamp.net/"); } Synchronous method in Desktop application
  • 11. CORECAMP Ivano-Frankivsk 2017 private async void StartButtonClick(object sender, RoutedEventArgs e) { string text = await Download(); TextBox.Text = text; } private async Task<string> Download() { return await new WebClient().DownloadStringTaskAsync("http://corecamp.net/"); } Asynchronous method in Desktop application
  • 12. CORECAMP Ivano-Frankivsk 2017 Evolution of Asynchronous concepts • Asynchronous Programming Model (APM) • Evented Asynchronous Programming (EAP) • Task-based Asynchronous Programming (TAP)
  • 13. CORECAMP Ivano-Frankivsk 2017 Asynchronous Programming Model (APM) // .NET 1 model file.BeginRead(buffer, 0, maxLength, asyncResult => { int numBytesRead = files.EndRead(asyncResult); // Now do something with "buffer" }, null);
  • 14. CORECAMP Ivano-Frankivsk 2017 Evented Asynchronous Programming (EAP) // .NET 2 model webClient.DownloadStringCompleted += (sender, args) => { string html = args.Result; // Now do someting with "html" }; webClient.DownloadStringAsync(new Uri("http://example.com"));
  • 15. CORECAMP Ivano-Frankivsk 2017 Task-based Asynchronous Programming (TAP) Task<string> htmlTask = webClient.DownloadStringTaskAsync(url); 1. string html = htmlTask.Result; // Sync (block until done) 2. htmlTask.ContinueWith(task => { string html = task.Result; // Async, C# 4 }); 3. string html = await htmlTask; // Async. C# 5
  • 17. Thank you for the attention!