5
Most read
8
Most read
15
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

PPTX
C# Async Await
PPTX
Async Programming in C# 5
PPTX
String, string builder, string buffer
PPTX
Asynchronous programming
PPTX
Nodejs functions & modules
PPTX
Java 8 - Features Overview
PPTX
Asynchronous Programming in .NET
PDF
JavaScript guide 2020 Learn JavaScript
C# Async Await
Async Programming in C# 5
String, string builder, string buffer
Asynchronous programming
Nodejs functions & modules
Java 8 - Features Overview
Asynchronous Programming in .NET
JavaScript guide 2020 Learn JavaScript

What's hot (20)

PPT
Asynchronous Programming in C# - Part 1
PDF
REST APIs with Spring
PPTX
Servlets
PPT
Asp.net.
PPTX
Introduction to angular with a simple but complete project
PDF
Java threads
PPTX
Introduction to Spring Framework
PDF
MVC Architecture
PPTX
Solid principles
PPTX
C# Arrays
PDF
Hibernate Presentation
PPTX
Modern JS with ES6
PPTX
Asp.net web api
PPT
Collection Framework in java
PPTX
Multiple inheritance in java3 (1).pptx
PPTX
Asp.Net Core MVC with Entity Framework
PPT
Java collections concept
PPTX
Getting started with typescript
PPTX
Spring data jpa
PPTX
Spring MVC
Asynchronous Programming in C# - Part 1
REST APIs with Spring
Servlets
Asp.net.
Introduction to angular with a simple but complete project
Java threads
Introduction to Spring Framework
MVC Architecture
Solid principles
C# Arrays
Hibernate Presentation
Modern JS with ES6
Asp.net web api
Collection Framework in java
Multiple inheritance in java3 (1).pptx
Asp.Net Core MVC with Entity Framework
Java collections concept
Getting started with typescript
Spring data jpa
Spring MVC
Ad

Similar to Asynchronous programming in C# (20)

PPTX
Asynchronous Programming.pptx
PPT
PPTX
AsynchronousProgrammingDesignPatterns.pptx
PDF
Asynchronous API in Java8, how to use CompletableFuture
PDF
Minko - Targeting Flash/Stage3D with C++ and GLSL
PDF
Apache Cassandra and Apache Spark
PPTX
Designing a machine learning algorithm for Apache Spark
PDF
Beginning MEAN Stack
PPTX
Js engine performance
PDF
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
PPTX
NodeJS guide for beginners
PDF
Solvcon PyCon APAC 2014
PDF
(1) c sharp introduction_basics_dot_net
PPTX
Java programming(unit 1)
PDF
PPTX
Intro to programing with java-lecture 1
PDF
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
PDF
Composer Helpdesk
PPTX
Let's play with ASP.NET 5 (vNext) RC1
PDF
Power-Efficient Programming Using Qualcomm Multicore Asynchronous Runtime Env...
Asynchronous Programming.pptx
AsynchronousProgrammingDesignPatterns.pptx
Asynchronous API in Java8, how to use CompletableFuture
Minko - Targeting Flash/Stage3D with C++ and GLSL
Apache Cassandra and Apache Spark
Designing a machine learning algorithm for Apache Spark
Beginning MEAN Stack
Js engine performance
Android Asynctask Internals vis-a-vis half-sync half-async design pattern
NodeJS guide for beginners
Solvcon PyCon APAC 2014
(1) c sharp introduction_basics_dot_net
Java programming(unit 1)
Intro to programing with java-lecture 1
.NET Fest 2018. Владимир Крамар. Многопоточное и асинхронное программирование...
Composer Helpdesk
Let's play with ASP.NET 5 (vNext) RC1
Power-Efficient Programming Using Qualcomm Multicore Asynchronous Runtime Env...
Ad

More from Bohdan Pashkovskyi (10)

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

Recently uploaded (20)

PPTX
Topic 5 Presentation 5 Lesson 5 Corporate Fin
PPTX
eGramSWARAJ-PPT Training Module for beginners
PPTX
retention in jsjsksksksnbsndjddjdnFPD.pptx
PDF
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
PDF
Best Data Science Professional Certificates in the USA | IABAC
PPTX
Caseware_IDEA_Detailed_Presentation.pptx
PPT
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
PPTX
Phase1_final PPTuwhefoegfohwfoiehfoegg.pptx
PPTX
1 hour to get there before the game is done so you don’t need a car seat for ...
PPTX
chrmotography.pptx food anaylysis techni
PPTX
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
PPTX
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
PPT
statistics analysis - topic 3 - describing data visually
PPTX
statsppt this is statistics ppt for giving knowledge about this topic
PPTX
MBA JAPAN: 2025 the University of Waseda
PPTX
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
PDF
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
PDF
Systems Analysis and Design, 12th Edition by Scott Tilley Test Bank.pdf
PDF
An essential collection of rules designed to help businesses manage and reduc...
PPTX
New ISO 27001_2022 standard and the changes
Topic 5 Presentation 5 Lesson 5 Corporate Fin
eGramSWARAJ-PPT Training Module for beginners
retention in jsjsksksksnbsndjddjdnFPD.pptx
OneRead_20250728_1808.pdfhdhddhshahwhwwjjaaja
Best Data Science Professional Certificates in the USA | IABAC
Caseware_IDEA_Detailed_Presentation.pptx
lectureusjsjdhdsjjshdshshddhdhddhhd1.ppt
Phase1_final PPTuwhefoegfohwfoiehfoegg.pptx
1 hour to get there before the game is done so you don’t need a car seat for ...
chrmotography.pptx food anaylysis techni
DS-40-Pre-Engagement and Kickoff deck - v8.0.pptx
FMIS 108 and AISlaudon_mis17_ppt_ch11.pptx
statistics analysis - topic 3 - describing data visually
statsppt this is statistics ppt for giving knowledge about this topic
MBA JAPAN: 2025 the University of Waseda
sac 451hinhgsgshssjsjsjheegdggeegegdggddgeg.pptx
Votre score augmente si vous choisissez une catégorie et que vous rédigez une...
Systems Analysis and Design, 12th Edition by Scott Tilley Test Bank.pdf
An essential collection of rules designed to help businesses manage and reduc...
New ISO 27001_2022 standard and the changes

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!