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
Jsp ppt
Vikas Jagtap
 
PDF
Java Basic Oops Concept
atozknowledge .com
 
PDF
Spring Framework - AOP
Dzmitry Naskou
 
PPTX
Concurrency and Parallelism, Asynchronous Programming, Network Programming
Prabu U
 
PPT
TypeScript Presentation
Patrick John Pacaña
 
PDF
TypeScript Introduction
Dmitry Sheiko
 
PPTX
Async programming in c#
Ahasanul Kalam Akib
 
PPTX
Asp.net MVC training session
Hrichi Mohamed
 
PPTX
Spring boot
sdeeg
 
ODP
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
PPTX
Laravel Tutorial PPT
Piyush Aggarwal
 
PPTX
Namespaces in C#
yogita kachve
 
PDF
TypeScript
Saray Chak
 
PPTX
NodeJS - Server Side JS
Ganesh Kondal
 
PDF
How to implement internationalization (i18n) in angular application(multiple ...
Katy Slemon
 
PDF
REST API and CRUD
Prem Sanil
 
PDF
Basics of JavaScript
Bala Narayanan
 
DOCX
Exceptions handling notes in JAVA
Sunil Kumar Gunasekaran
 
PDF
TypeScript - An Introduction
NexThoughts Technologies
 
PPTX
Multi-threaded Programming in JAVA
Vikram Kalyani
 
Jsp ppt
Vikas Jagtap
 
Java Basic Oops Concept
atozknowledge .com
 
Spring Framework - AOP
Dzmitry Naskou
 
Concurrency and Parallelism, Asynchronous Programming, Network Programming
Prabu U
 
TypeScript Presentation
Patrick John Pacaña
 
TypeScript Introduction
Dmitry Sheiko
 
Async programming in c#
Ahasanul Kalam Akib
 
Asp.net MVC training session
Hrichi Mohamed
 
Spring boot
sdeeg
 
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
Laravel Tutorial PPT
Piyush Aggarwal
 
Namespaces in C#
yogita kachve
 
TypeScript
Saray Chak
 
NodeJS - Server Side JS
Ganesh Kondal
 
How to implement internationalization (i18n) in angular application(multiple ...
Katy Slemon
 
REST API and CRUD
Prem Sanil
 
Basics of JavaScript
Bala Narayanan
 
Exceptions handling notes in JAVA
Sunil Kumar Gunasekaran
 
TypeScript - An Introduction
NexThoughts Technologies
 
Multi-threaded Programming in JAVA
Vikram Kalyani
 

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
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 
PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PPTX
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 
PPTX
Powerful Uses of Data Analytics You Should Know
subhashenia
 
PDF
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
PDF
apidays Singapore 2025 - Surviving an interconnected world with API governanc...
apidays
 
PPTX
05_Jelle Baats_Tekst.pptx_AI_Barometer_Release_Event
FinTech Belgium
 
PDF
The Best NVIDIA GPUs for LLM Inference in 2025.pdf
Tamanna36
 
PPTX
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
PPTX
big data eco system fundamentals of data science
arivukarasi
 
PDF
JavaScript - Good or Bad? Tips for Google Tag Manager
📊 Markus Baersch
 
PDF
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
PPTX
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
PDF
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 
PDF
InformaticsPractices-MS - Google Docs.pdf
seshuashwin0829
 
PPTX
How to Add Columns and Rows in an R Data Frame
subhashenia
 
PPTX
SHREYAS25 INTERN-I,II,III PPT (1).pptx pre
swapnilherage
 
PDF
Data Science Course Certificate by Sigma Software University
Stepan Kalika
 
PDF
Research Methodology Overview Introduction
ayeshagul29594
 
PDF
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
apidays
 
Driving Employee Engagement in a Hybrid World.pdf
Mia scott
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
apidays Singapore 2025 - The Quest for the Greenest LLM , Jean Philippe Ehre...
apidays
 
Powerful Uses of Data Analytics You Should Know
subhashenia
 
apidays Singapore 2025 - How APIs can make - or break - trust in your AI by S...
apidays
 
apidays Singapore 2025 - Surviving an interconnected world with API governanc...
apidays
 
05_Jelle Baats_Tekst.pptx_AI_Barometer_Release_Event
FinTech Belgium
 
The Best NVIDIA GPUs for LLM Inference in 2025.pdf
Tamanna36
 
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
big data eco system fundamentals of data science
arivukarasi
 
JavaScript - Good or Bad? Tips for Google Tag Manager
📊 Markus Baersch
 
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
b6057ea5-8e8c-4415-90c0-ed8e9666ffcd.pptx
Anees487379
 
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 
InformaticsPractices-MS - Google Docs.pdf
seshuashwin0829
 
How to Add Columns and Rows in an R Data Frame
subhashenia
 
SHREYAS25 INTERN-I,II,III PPT (1).pptx pre
swapnilherage
 
Data Science Course Certificate by Sigma Software University
Stepan Kalika
 
Research Methodology Overview Introduction
ayeshagul29594
 
apidays Singapore 2025 - Trustworthy Generative AI: The Role of Observability...
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!