SlideShare a Scribd company logo
1 of 15
ASYNCHRONOUS PROGRAMMING
Code Review 4/26/13 – Chester Hartin
Hopes
 Identify where you may need asynchronous
processing
 Implement it with Background workers,Task
Parallel Library, or async & await pattern
 Learn to refactor synchronous applications
into become more responsive
Asynchronous vs. Parallel
 Who remembers my talk about boiling eggs
using the parallel extensions?
 Concurrency / Boiling several eggs simultaneously
 By boiling the eggs in different pots, we are
boiling them in parallel to each other.
 Now if we do something else while they’re
boiling, like cleaning up the kitchen, and we
go back to them once they’re done… that
task is done asynchronously
Asynchronous vs. Parallel
 Think of parallelism as something that runs
independently at the same time as another
task
 Think of asynchronous operations as
something that will let you do things in the
meantime while you wait, but once it’s
finished, you receive the results
Asynchronous
 It’s common to look at asynchronous
processes as non-blocking (ie the system
won’t lock up, will continue).
 So how do we do this?
Synchronous Design
Asynchronous Design
Asynchronous
 There are several methods
 Threads &Thread Pooling
 Background workers
 Task Parallel Library (TPL)
 Asynch & await
Example 1 – No threading
 lblInfo.Text = ProcessOrder();
private string ProcessOrder() {
// really long task that takes for ever
}
Example 2 – background worker
 //ex2
 private void bgw_DoWork(object sender, DoWor
kEventArgs e)
 { e.Result = ProcessOrder(); }
 private void bgw_RunWorkerCompleted(object s
ender, RunWorkerCompletedEventArgs e)
 { lblInfo.Text = e.Result.ToString() ; }
Example 3 – TPL (Task Parallel Library )
 private void ProcessOrderContinueWith()
{
var task = newTask<string>(ProcessOrder);
task.Start();
task.ContinueWith((parentTask) => {
this.Invoke(new Action(() => {
lblInfo.Text = parentTask.Result;}));});
lblInfo.Text = "Thinking";
}
Example 3 – TPL (cont)
 What happens is we’re creating a State
machine
 This handles everything in the background &
keeps track of what task is running & where
so it knows where to go back
Example 4 -- Async & Await
private async void ProcessOrderAsync()
{
var processOrderResult =
Task<string>.Factory.StartNew(ProcessOrder);
lblInfo.Text = await processOrderResult;
}
Example 4 -- Async & Await
 Still a state machine, but way easier to read &
implement!
Refactoring
 Using async & await allows you to utilize
most of your existing code

More Related Content

What's hot

Vagrant are you still develop in a non-virtual environment-
Vagrant  are you still develop in a non-virtual environment-Vagrant  are you still develop in a non-virtual environment-
Vagrant are you still develop in a non-virtual environment-Anatoly Bubenkov
 
Learn watchOS Programming!
Learn watchOS Programming! Learn watchOS Programming!
Learn watchOS Programming! Snehal Patil
 
Gnocchi v3 brownbag
Gnocchi v3 brownbagGnocchi v3 brownbag
Gnocchi v3 brownbagGordon Chung
 
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017Codemotion
 

What's hot (7)

Vagrant are you still develop in a non-virtual environment-
Vagrant  are you still develop in a non-virtual environment-Vagrant  are you still develop in a non-virtual environment-
Vagrant are you still develop in a non-virtual environment-
 
Learn watchOS Programming!
Learn watchOS Programming! Learn watchOS Programming!
Learn watchOS Programming!
 
Liferay OpenShift base concepts
Liferay OpenShift base conceptsLiferay OpenShift base concepts
Liferay OpenShift base concepts
 
Secure code 3rd_party_libs
Secure code 3rd_party_libsSecure code 3rd_party_libs
Secure code 3rd_party_libs
 
Gnocchi v3 brownbag
Gnocchi v3 brownbagGnocchi v3 brownbag
Gnocchi v3 brownbag
 
竝行
竝行竝行
竝行
 
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
 

Similar to Asynchronous programming

Asynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionAsynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionPyData
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/MultitaskingSasha Kravchuk
 
Async and Await on the Server
Async and Await on the ServerAsync and Await on the Server
Async and Await on the ServerDoug Jones
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and pythonChetan Giridhar
 
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLAsync and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLArie Leeuwesteijn
 
Async-await best practices in 10 minutes
Async-await best practices in 10 minutesAsync-await best practices in 10 minutes
Async-await best practices in 10 minutesPaulo Morgado
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Baruch Sadogursky
 
Classic synchronization
Classic synchronizationClassic synchronization
Classic synchronizationhina firdaus
 
Task parallel library presentation
Task parallel library presentationTask parallel library presentation
Task parallel library presentationahmed sayed
 
End to-end async and await
End to-end async and awaitEnd to-end async and await
End to-end async and awaitvfabro
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...julien.ponge
 
Presentation: Everything you wanted to know about writing async, high-concurr...
Presentation: Everything you wanted to know about writing async, high-concurr...Presentation: Everything you wanted to know about writing async, high-concurr...
Presentation: Everything you wanted to know about writing async, high-concurr...Baruch Sadogursky
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot NetNeeraj Kaushik
 
Asynchronous in dot net4
Asynchronous in dot net4Asynchronous in dot net4
Asynchronous in dot net4Wei Sun
 
C# Parallel programming
C# Parallel programmingC# Parallel programming
C# Parallel programmingUmeshwaran V
 
Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)Yoshifumi Kawai
 

Similar to Asynchronous programming (20)

Asynchronous Python A Gentle Introduction
Asynchronous Python A Gentle IntroductionAsynchronous Python A Gentle Introduction
Asynchronous Python A Gentle Introduction
 
.NET Multithreading/Multitasking
.NET Multithreading/Multitasking.NET Multithreading/Multitasking
.NET Multithreading/Multitasking
 
Async and Await on the Server
Async and Await on the ServerAsync and Await on the Server
Async and Await on the Server
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
 
AsyncIO To Speed Up Your Crawler
AsyncIO To Speed Up Your CrawlerAsyncIO To Speed Up Your Crawler
AsyncIO To Speed Up Your Crawler
 
Training – Going Async
Training – Going AsyncTraining – Going Async
Training – Going Async
 
Async and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NLAsync and parallel patterns and application design - TechDays2013 NL
Async and parallel patterns and application design - TechDays2013 NL
 
Async-await best practices in 10 minutes
Async-await best practices in 10 minutesAsync-await best practices in 10 minutes
Async-await best practices in 10 minutes
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java
 
Classic synchronization
Classic synchronizationClassic synchronization
Classic synchronization
 
Task parallel library presentation
Task parallel library presentationTask parallel library presentation
Task parallel library presentation
 
storm-170531123446.pptx
storm-170531123446.pptxstorm-170531123446.pptx
storm-170531123446.pptx
 
End to-end async and await
End to-end async and awaitEnd to-end async and await
End to-end async and await
 
concurrency
concurrencyconcurrency
concurrency
 
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
Java 7 Launch Event at LyonJUG, Lyon France. Fork / Join framework and Projec...
 
Presentation: Everything you wanted to know about writing async, high-concurr...
Presentation: Everything you wanted to know about writing async, high-concurr...Presentation: Everything you wanted to know about writing async, high-concurr...
Presentation: Everything you wanted to know about writing async, high-concurr...
 
Parallel Programming With Dot Net
Parallel Programming With Dot NetParallel Programming With Dot Net
Parallel Programming With Dot Net
 
Asynchronous in dot net4
Asynchronous in dot net4Asynchronous in dot net4
Asynchronous in dot net4
 
C# Parallel programming
C# Parallel programmingC# Parallel programming
C# Parallel programming
 
Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)Deep Dive async/await in Unity with UniTask(EN)
Deep Dive async/await in Unity with UniTask(EN)
 

More from Chester Hartin (8)

Xamarin 101
Xamarin 101Xamarin 101
Xamarin 101
 
Proxy pattern
Proxy patternProxy pattern
Proxy pattern
 
Elapsed time
Elapsed timeElapsed time
Elapsed time
 
Dependency injection
Dependency injectionDependency injection
Dependency injection
 
Map kit
Map kitMap kit
Map kit
 
Hash tables
Hash tablesHash tables
Hash tables
 
Parallel extensions
Parallel extensionsParallel extensions
Parallel extensions
 
Reflection
ReflectionReflection
Reflection
 

Asynchronous programming

  • 1. ASYNCHRONOUS PROGRAMMING Code Review 4/26/13 – Chester Hartin
  • 2. Hopes  Identify where you may need asynchronous processing  Implement it with Background workers,Task Parallel Library, or async & await pattern  Learn to refactor synchronous applications into become more responsive
  • 3. Asynchronous vs. Parallel  Who remembers my talk about boiling eggs using the parallel extensions?  Concurrency / Boiling several eggs simultaneously  By boiling the eggs in different pots, we are boiling them in parallel to each other.  Now if we do something else while they’re boiling, like cleaning up the kitchen, and we go back to them once they’re done… that task is done asynchronously
  • 4. Asynchronous vs. Parallel  Think of parallelism as something that runs independently at the same time as another task  Think of asynchronous operations as something that will let you do things in the meantime while you wait, but once it’s finished, you receive the results
  • 5. Asynchronous  It’s common to look at asynchronous processes as non-blocking (ie the system won’t lock up, will continue).  So how do we do this?
  • 8. Asynchronous  There are several methods  Threads &Thread Pooling  Background workers  Task Parallel Library (TPL)  Asynch & await
  • 9. Example 1 – No threading  lblInfo.Text = ProcessOrder(); private string ProcessOrder() { // really long task that takes for ever }
  • 10. Example 2 – background worker  //ex2  private void bgw_DoWork(object sender, DoWor kEventArgs e)  { e.Result = ProcessOrder(); }  private void bgw_RunWorkerCompleted(object s ender, RunWorkerCompletedEventArgs e)  { lblInfo.Text = e.Result.ToString() ; }
  • 11. Example 3 – TPL (Task Parallel Library )  private void ProcessOrderContinueWith() { var task = newTask<string>(ProcessOrder); task.Start(); task.ContinueWith((parentTask) => { this.Invoke(new Action(() => { lblInfo.Text = parentTask.Result;}));}); lblInfo.Text = "Thinking"; }
  • 12. Example 3 – TPL (cont)  What happens is we’re creating a State machine  This handles everything in the background & keeps track of what task is running & where so it knows where to go back
  • 13. Example 4 -- Async & Await private async void ProcessOrderAsync() { var processOrderResult = Task<string>.Factory.StartNew(ProcessOrder); lblInfo.Text = await processOrderResult; }
  • 14. Example 4 -- Async & Await  Still a state machine, but way easier to read & implement!
  • 15. Refactoring  Using async & await allows you to utilize most of your existing code

Editor's Notes

  1. Using a background worker certainly works, but the code will easily get messy and it is not really a linear way of writing code.