SlideShare a Scribd company logo
1 of 49
GOING ASYNC 
THERE IS NO THREAT 
MAXIME LEMAITRE – 29/10/14
Agenda 
• Introduction 
• Brief History 
• Refresher of the TPL 
• What’s different since 4.5 ? 
• Best Practices 
• Working with Async 
Asynchronous programming is a 
means of parallel programming in 
which a unit of work runs 
separately from the main 
application thread and notifies the 
calling thread of its completion, 
failure or progress. Why do we 
need it now ?
Dangers of Synchronous programming 
Client-Side Server-Side
What does asynchrony really mean? 
Synchronous 
Perform something here and now. 
I’ll regain control to execute something 
else when it’s done. 
Asynchronous 
Initiate something here and now. 
I’ll regain control to execute something else 
“immediately”. 
How an operation is invoked 
implies nothing about the 
implementation of the workload itself. 
Async Clinic
Sync vs Async 
“Pause for 10 seconds, then output 'Hello' to the console.” 
Synchronous 
Asynchronous 
Both “async over sync” and “sync over async” can be problematic 
if you don’t completely understand the implementation.
Why go Async ?
Going Async 
public class MyClass 
{ 
public int Read(byte [] buffer, int offset, int count); 
} 
public class MyClass 
{ 
public Task<int> ReadAsync(byte [] buffer, int offset, int count); 
} 
10 years 
1.0 (2002) 
4.5 (2012)
Asynchronous Programming Model (APM) 
http://msdn.microsoft.com/en-us/library/ms228963.aspx 
• Introduced in .net 1.1 (no async support before) 
• Asynchronous operations require BeginXX and EndXX methods 
– Example : BeginWrite and EndWrite for asynchronous write operations). 
• No longer recommended 
• Several APIs and legacy code still use this pattern 
public class MyClass 
{ 
public IAsyncResult BeginRead( 
byte [] buffer, int offset, int count, 
AsyncCallback callback, object state); 
public int EndRead(IAsyncResult asyncResult); 
}
Event-based Asynchronous Pattern (EAP) 
http://msdn.microsoft.com/en-us/library/ms228969.aspx 
• Introduced in .net 2.0 
• Requires a method that has the Async suffix, and also requires 
one or more events, event handler delegate types, and 
EventArg-derived types. 
public class MyClass 
{ 
public void ReadAsync(byte [] buffer, int offset, int count); 
public event ReadCompletedEventHandler ReadCompleted; 
} 
public delegate void ReadCompletedEventHandler( 
object sender, ReadCompletedEventArgs eventArgs); 
public class ReadCompletedEventArgs : AsyncCompletedEventArgs 
{ 
public int Result { get; } 
}
Task-based Asynchronous Pattern (TAP) 
http://msdn.microsoft.com/en-us/library/hh873175.aspx 
• Introduced in .net 4.0 but greatly improved in 4.5 
• Uses a single method to represent the initiation and 
completion of an asynchronous operation. 
• Recommended approach to asynchronous programming in 
the .NET Framework. 
• Very similar to the synchronous version 
• Interop with Other Asynchronous Patterns and Types is 
supported (EAP<>TAP, APM<>TAP) 
public class MyClass 
{ 
public Task<int> ReadAsync(byte [] buffer, int offset, int count); 
}
Refresher on the TPL 
What is a Task/Task<> ?
What is a Task/Task<> ? 
• A Task represents an asynchronous operation 
– resembles a thread or ThreadPool work item, but at a higher level of abstraction 
– Similar to promises in Javascript 
• Task can only run from start to finish once 
– you cannot run the same task object two times. Have to create another Task object for 
running the same code 
• More efficient and more scalable use of system resources. 
– Tasks are queued to the ThreadPool, which has been enhanced with algorithms that 
determine and adjust to the number of threads and that provide load balancing to 
maximize throughput. This makes tasks relatively lightweight, and you can create many 
of them to enable fine-grained parallelism. 
• More programmatic control than is possible with a thread or work item. 
– Rich set of APIs that support waiting, cancellation, continuations, robust exception 
handling, detailed status, custom scheduling, and more. 
• Task (as it is used by the TPL) is pretty much completely different than Task (as it is 
used by async). 
TPL is the preferred API for writing multi-threaded, 
asynchronous, and parallel code
Task<T> Properties 
Task<T> has important properties. 
Don’t reinvent the wheel when consuming Task and Task<T> !
Starting a Task 
• Threre are many ways to start a task. 
• Most of time, we don’t need to start a Task 
by yourself ; The API/Framework method 
will give a task. 
– eg HttpClient, StreamReader, … 
• If you still need to start a Task, use 
– Task.Run 
– Task.Factory.StartNew for a full control 
• About Task.Run 
– Do not use it just to “provide something 
awaitable” aka a fake-asynchronous method 
– CPU-bound tasks only 
Task.Run vs Task.Factory.StartNew
Cancelling a Task 
• Cancellation is supported through the use of 
CancellationToken (via 
CancellationTokenSource ) to request 
cancellation 
• You can terminate an operation 
– By returning the delegate (In the case the 
status is RantoCompletion and not Canceled) 
– By throwing a OperationCanceledException via 
ThrowIfCancellationRequested 
• Cancellation token are present in all async API 
Task Cancellation
Exceptions handling 
• User-code can throw intentionally (or not 
any) any kind of exceptions. 
• Exceptions are propagated back to the 
joining code when using Task.Wait(), 
Task.Result,await … 
– Be sure to always surround this with a 
try/catch statement 
• Task infrastructure wraps thrown 
exceptions in AggregateException 
instance. In that case, the task status is 
Faulted and Task.Exception contains 
Read more on TPL and Exceptions
Task Continuations 
• A continuation task is an asynchronous task that is invoked by another task 
– Traditionally, this has been done by using callback methods 
• Continuations are relatively easy to use, but are nevertheless very powerful and 
flexible. For example, you can: 
– pass data from the antecedent to the continuation 
– specify the conditions under which the continuation will be invoked or not invoked 
– cancel a continuation either before it starts or cooperatively as it is running 
– invoke multiple continuations from the same antecedent 
– invoke one continuation when all or any one of multiple antecedents complete 
– chain continuations one after another to any arbitrary length 
– use a continuation to handle exceptions thrown by the antecedent 
– … 
• Many continuations options (NotOnFaulted, OnlyOnFaulted, ..) 
• Continuation Tasks is an important topic to achieve « Async all the way » 
Continuation Tasks
Task Continuation 
Examples
What’s different 
since .net 4.5 ?
What’s different now ? 
Asynchronous programming 
with .NET 4 is a little easier. 
Asynchronous programming 
with .NET 4.5 is a lot easier.
Task = new Task 
TPL 4.5 Performance improvements
TPL at the Core layer 
Application area Supporting APIs that contain async methods 
Web access HttpClient , SyndicationClient 
Working with files StorageFile, StreamWriter, StreamReader, XmlReader 
Working with images MediaCapture, BitmapEncoder, BitmapDecoder 
WCF programming Synchronous and Asynchronous Operations 
Asynchrony is essential for activities that are potentially 
blocking, ; so async programming was added in several APIs 
from the .NET Framework 4.5 and the Windows Runtime
New Task Methods 
• Task.Run : Use it to offload work as a Task or Task<TResult> to the thread pool 
• Task.Delay : Use the Delay method to introduce pauses into an asynchronous 
method’s execution 
• Task.FromResult : Use the FromResult<TResult> method in scenarios where data 
may already be available and just needs to be returned from a task-returning 
method lifted into a Task<TResult>: 
• Task.WhenAll : Use the WhenAll method to asynchronously wait on multiple 
asynchronous operations that are represented as tasks 
• Task.WhenAny : Use the WhenAny method to asynchronously wait for just one of 
multiple asynchronous operations represented as tasks to complete 
• …
New keywords : async/await 
added in .net 4.5 & C# 5 
• Allow writing/debugging async code almost as if it is a usual synchronous code 
• Both keywords - async and await - always work together. 
– await without async is not allowed. 
– async without await is allowed, but not the method executes as a synchronous method 
Use the async modifier to specify that a 
method, lambda expression, or anonymous 
method is asynchronous. 
The async keyword was mainly added to 
avoid backwards compatibility problems 
when using the await keyword 
An async method can have a return type of 
Task, Task<TResult>, or void 
The await operator is applied to a task in an 
asynchronous method to suspend the 
execution of the method until the awaited 
task completes
await/async control flow 
Control Flow in Async Programs
Await/await with .net 4.0 
• It’s possible to use async/await in projects targeting .net 4.0 
• It does not include the ASP.NET runtime changes necessary for 
proper async/await support, so if you install it into an ASP.NET 4.0 project, the 
code will compile but will not work correctly 
• It’s better to upgade to 4.5
There is no thread 
“if I am awaiting an operation, 
there must be a thread that is doing the 
wait! It’s probably a thread pool thread. 
Or an OS thread! Or something with a 
device driver…” 
There is no thread 
“Regardless of the type of I/O request, 
internally I/O operations issued to a driver 
on behalf of the application are 
performed asynchronously”, 
Windows Internals
Working with TPL
How do I know which method is async or not ? 
The .NET Framework 4.5 contains many members that work with async and await. You 
can recognize these members by the "Async" suffix that’s attached to the member 
name and a return type of Task or Task<TResult>. 
Library methods shouldn’t lie. 
Be honest. Use “XxAsync” if, and only if, you’re 
not thread-bound (with a few notable 
exceptions). 
Suffix should help caller to understand 
implementation.
TAP Guidelines 
There are many new await-friendly techniques that should be used instead of the old 
blocking techniques. If you have any of these Old examples in your new async code, 
you’re Doing It Wrong. TAP Guidelines
Async all the way 
• “await and async” is poison. 
– Await/await isn’t intended to be used in just a single function, but for an entire flow in 
your application. 
– Once you will add them somewhere in your code. These keywords will propagate all of 
the way up to the top of a logical stack. 
• Asynchronous code works best if asynchronous code calls and is called by other 
asynchronous code 
• Don’t mix synchronous and asynchronous code (without carefully considering the 
consequences) 
– bad idea to block on async code by calling Task.Wait or Task.Result. 
– Mixed async and blocking code can cause deadlocks, more-complex error handling and 
unexpected blocking of context threads 
• Exceptions 
– Console Applications, Unit Tests
Asynchronous wrappers for synchronous methods? 
“async over sync” 
• Two primary benefits to asynchrony: scalability and offloading (e.g. 
responsiveness, parallelism) 
• Scalability 
– still consuming the same amount of resources (even a bit more) 
– Don’t put Task.Run everywhere to achieve async over sync. Use a thread from the 
thread pool. 
• Offloading 
– if a consumer of your library wants to use the synchronous method asynchronously, 
they can do so on their own 
Async 
http://blogs.msdn.com/b/pfxteam/archive/2012/03/24/10287244.aspx
Synchronous wrappers for asynchronous methods? 
“sync over async” 
• Avoid exposing a synchronous method that just wraps the asynchronous 
implementation 
– hides from the consumer the true nature of the implementation 
– If the consumer chooses to block waiting for the asynchronous 
implementation to complete, that’s up to the caller 
• Can lead to significant problems with the application, such as hangs 
http://blogs.msdn.com/b/pfxteam/archive/2012/04/13/10293638.aspx
Async with asp.net (API or MVC) 
What benefits ? 
Introduction to Async/Await on ASP.NET 
In the case of I/O bound operations (whether that's disk 
I/O or network I/O), the waiting that a thread might 
normally do is purposeless. Using an async version of the 
I/O method allows the worker thread -- which are 
generally in limited supply -- to go back into the worker 
pool to start servicing another request. When the I/O is 
ready, and a thread from the worker pool is available, 
then the original request can be completed. This can have 
a fairly substantial benefit in scalability of the server, 
because it means servicing lots more requests with far 
fewer resources. 
Don’t forget Parallelization : do not call 
sequentially N operations but parallelize 
work, and therefore return the data to the 
client sooner
Tasks and asp.net MVC 
The way to build asynchronous controllers has been completely changed compared to 
how it was done previously with the AsyncController superclass. This class and the 
inherent complexity in using it are gone. 
Async
Task in asp.net Web Api 
Mark your Api action with 
async + Task<Result> 
Message handlers also 
support async/await. 
(Cleaner than writing it 
with a continuation task).
TPL Best Practices
Async is for “long” tasks 
As there is a small overhead of using async (SynchronizationContext, Task Creation, 
Task Scheduling, Queueing…) ; so Do not put async everwhere. It won’t help. 
• Use where .NET 4.5 framework is Async 
– Network I/O 
– File I/O 
– Database I/O 
• CAUTION! (not all providers are async) 
– Remote Service Calls 
• Use for CPU-bound work 
– > 50ms with a user waiting 
– For server code, it depends
Consider using await instead of ContinueWith/Unwrap 
• Async/await is better 
– Easier to read 
– Fewer context switches 
– Better memory footprint – 
especially with chained tasks 
– Less startup overhead 
TPL 4.5 Performance improvements
Use cached Tasks when possible 
“While we have strived to slim down Task and 
eliminate unnecessary allocations in .NET 4.5, the 
optimal allocation is one that never happens. 
Sometimes it is possible to cache a single instance of 
a frequently used Task, eliminating the need to 
continually re-allocate the Task.”, Stephen Toub 
• Cache the Tasks, not the Data 
• reuse completed tasks 
• Use completed & static Tasks 
– Use Task.FromResult
Encode closure information into a Task’s state object 
Compilation 
• Pass values by parameter where possible 
• Only capture the variable you are going to use. 
– Use local variables when necessary
Don’t block on async code 
UI Client UI Asp.net 
Deadlock ! Why ? 
• After you await a Task, when the method continues it will continue in a context. 
• GUI and ASP.NET applications have a SynchronizationContext that permits only 
one chunk of code to run at a time 
Don’t block on async code
Don't block on async code 
Call ConfigureAwait 
• Don’t Wait on Tasks 
– Let the runtime schedule the continuations 
• Don’t access Task.Result unless it’s completed 
– Task.Result is blocking 
• If you must, be aware of synchronized contexts: 
– UI Threads 
– ASP.NET Threads 
• For API developpers 
Don’t block on Tasks; use async all 
the way down 
– Call ConfigureAwait(false) It’s all about SynchronizationContext
Use Async void for event Handlers 
void is a possible return type (with Task and Task<Result>) for an async method 
• Principles 
– Async void is a “fire-and-forget” mechanism... 
– The caller is unable to know when an async void has finished 
– The caller is unable to catch exceptions thrown from an async void 
• Guidance 
– Use async void methods only for top-level event handlers (and their like) 
– Use async Task-returning methods everywhere else 
– When you see an async lambda, verify it 
Async void Tips
TPL Dataflow 
• TPL DataFlow is a library for building asynchronous data processing 
application 
• Not distributed with the .NET Framework 4.5, avaible via Nuget 
• A natural extension of the TPL library that allows developers to create 
data-processing pipelines in their applications 
• Many concepts of Actor model 
TPL Dataflow on MSDN
Not Async 
Not Running 
Asynchronously. Why ? 
‘async’ don’t forks. 
Use Task.Run to offload
Method not completing 
Awaited task 
sometimes never 
completes. Why ? 
Always complete Tasks 
when the underlying 
operation has ended 
Deadlocking UI thread. 
Why ? 
Don’t synchronously wait on the UI thread. 
Use ConfigureAwait(false) whenever possible.
Questions
References 
• http://www.codeproject.com/Articles/518856/Task-Parallel-Library-and-async-await-Functionalit 
• http://msdn.microsoft.com/en-us/library/dd997423(v=vs.110).aspx 
• http://www.asp.net/aspnet/overview/developing-apps-with-windows-azure/building-real-world-cloud-apps- 
with-windows-azure/web-development-best-practices#sessionstate 
• http://blog.stephencleary.com/ 
• http://msdn.microsoft.com/en-us/magazine/jj991977.aspx 
• http://msdn.microsoft.com/fr-fr/magazine/dn802603(en-us).aspx 
• http://bradwilson.typepad.com/blog/2012/04/tpl-and-servers-pt1.html 
• http://download.microsoft.com/download/5/B/9/5B924336-AA5D-4903-95A0-56C6336E32C9/TAP.docx 
• http://blogs.msdn.com/b/dotnet/archive/2012/04/03/async-in-4-5-worth-the-await.aspx 
• http://msdn.microsoft.com/en-us/magazine/gg598924.aspx 
• http://msdn.microsoft.com/en-us/library/hh191443.aspx 
• http://www.codeproject.com/Articles/562021/Asynchronous-models-and-patterns#await-async 
• http://www.codeproject.com/Articles/518856/Task-Parallel-Library-and-async-await-Functionalit 
• http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx 
• http://msdn.microsoft.com/en-us/library/hh873173.aspx 
• http://www.microsoft.com/en-us/download/confirmation.aspx?id=19957 
• http://code.jonwagner.com/2012/09/06/best-practices-for-c-asyncawait/

More Related Content

What's hot

Streams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to RxStreams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to RxAndrzej Sitek
 
Reactive programming and Hystrix fault tolerance by Max Myslyvtsev
Reactive programming and Hystrix fault tolerance by Max MyslyvtsevReactive programming and Hystrix fault tolerance by Max Myslyvtsev
Reactive programming and Hystrix fault tolerance by Max MyslyvtsevJavaDayUA
 
Reactive Micro Services with Java seminar
Reactive Micro Services with Java seminarReactive Micro Services with Java seminar
Reactive Micro Services with Java seminarGal Marder
 
PuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, Instruct
PuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, InstructPuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, Instruct
PuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, InstructPuppet
 
The Future starts with a Promise
The Future starts with a PromiseThe Future starts with a Promise
The Future starts with a PromiseAlexandru Nedelcu
 
Monitoring with Prometheus
Monitoring with Prometheus Monitoring with Prometheus
Monitoring with Prometheus Pravin Magdum
 
Open source tools for optimizing your peering infrastructure @ DE-CIX TechMee...
Open source tools for optimizing your peering infrastructure @ DE-CIX TechMee...Open source tools for optimizing your peering infrastructure @ DE-CIX TechMee...
Open source tools for optimizing your peering infrastructure @ DE-CIX TechMee...Daniel Czerwonk
 
FunctionalConf '16 Robert Virding Erlang Ecosystem
FunctionalConf '16 Robert Virding Erlang EcosystemFunctionalConf '16 Robert Virding Erlang Ecosystem
FunctionalConf '16 Robert Virding Erlang EcosystemRobert Virding
 
Python Raster Function - Esri Developer Conference - 2015
Python Raster Function - Esri Developer Conference - 2015Python Raster Function - Esri Developer Conference - 2015
Python Raster Function - Esri Developer Conference - 2015akferoz07
 
面向引擎——编写高效率JS
面向引擎——编写高效率JS面向引擎——编写高效率JS
面向引擎——编写高效率JSucarticle
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with EpsilonSina Madani
 
The Actor Model - Towards Better Concurrency
The Actor Model - Towards Better ConcurrencyThe Actor Model - Towards Better Concurrency
The Actor Model - Towards Better ConcurrencyDror Bereznitsky
 
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
 Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark... Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...Databricks
 
Communicating with Scientific (Current)
Communicating with Scientific (Current)Communicating with Scientific (Current)
Communicating with Scientific (Current)Matthew Herndon
 
Parallel Programming in Python: Speeding up your analysis
Parallel Programming in Python: Speeding up your analysisParallel Programming in Python: Speeding up your analysis
Parallel Programming in Python: Speeding up your analysisManojit Nandi
 

What's hot (20)

Streams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to RxStreams, Streams Everywhere! An Introduction to Rx
Streams, Streams Everywhere! An Introduction to Rx
 
Reactive programming and Hystrix fault tolerance by Max Myslyvtsev
Reactive programming and Hystrix fault tolerance by Max MyslyvtsevReactive programming and Hystrix fault tolerance by Max Myslyvtsev
Reactive programming and Hystrix fault tolerance by Max Myslyvtsev
 
Reactive Micro Services with Java seminar
Reactive Micro Services with Java seminarReactive Micro Services with Java seminar
Reactive Micro Services with Java seminar
 
PuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, Instruct
PuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, InstructPuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, Instruct
PuppetConf 2017: No Server Left Behind - Miguel Di Ciurcio Filho, Instruct
 
Javantura v4 - Java and lambdas and streams - are they better than for loops ...
Javantura v4 - Java and lambdas and streams - are they better than for loops ...Javantura v4 - Java and lambdas and streams - are they better than for loops ...
Javantura v4 - Java and lambdas and streams - are they better than for loops ...
 
Server side story
Server side storyServer side story
Server side story
 
The Future starts with a Promise
The Future starts with a PromiseThe Future starts with a Promise
The Future starts with a Promise
 
Monitoring with Prometheus
Monitoring with Prometheus Monitoring with Prometheus
Monitoring with Prometheus
 
Variables in Pharo5
Variables in Pharo5Variables in Pharo5
Variables in Pharo5
 
Open source tools for optimizing your peering infrastructure @ DE-CIX TechMee...
Open source tools for optimizing your peering infrastructure @ DE-CIX TechMee...Open source tools for optimizing your peering infrastructure @ DE-CIX TechMee...
Open source tools for optimizing your peering infrastructure @ DE-CIX TechMee...
 
FunctionalConf '16 Robert Virding Erlang Ecosystem
FunctionalConf '16 Robert Virding Erlang EcosystemFunctionalConf '16 Robert Virding Erlang Ecosystem
FunctionalConf '16 Robert Virding Erlang Ecosystem
 
Python Raster Function - Esri Developer Conference - 2015
Python Raster Function - Esri Developer Conference - 2015Python Raster Function - Esri Developer Conference - 2015
Python Raster Function - Esri Developer Conference - 2015
 
Pycon 2008: Python Command-line Tools *Nix
Pycon 2008:  Python Command-line Tools *NixPycon 2008:  Python Command-line Tools *Nix
Pycon 2008: Python Command-line Tools *Nix
 
面向引擎——编写高效率JS
面向引擎——编写高效率JS面向引擎——编写高效率JS
面向引擎——编写高效率JS
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with Epsilon
 
The Actor Model - Towards Better Concurrency
The Actor Model - Towards Better ConcurrencyThe Actor Model - Towards Better Concurrency
The Actor Model - Towards Better Concurrency
 
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
 Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark... Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
Validating Big Data Jobs—Stopping Failures Before Production on Apache Spark...
 
Communicating with Scientific (Current)
Communicating with Scientific (Current)Communicating with Scientific (Current)
Communicating with Scientific (Current)
 
Javantura v4 - Java or Scala – Web development with Playframework 2.5.x - Kre...
Javantura v4 - Java or Scala – Web development with Playframework 2.5.x - Kre...Javantura v4 - Java or Scala – Web development with Playframework 2.5.x - Kre...
Javantura v4 - Java or Scala – Web development with Playframework 2.5.x - Kre...
 
Parallel Programming in Python: Speeding up your analysis
Parallel Programming in Python: Speeding up your analysisParallel Programming in Python: Speeding up your analysis
Parallel Programming in Python: Speeding up your analysis
 

Similar to Training – Going Async

Task parallel library presentation
Task parallel library presentationTask parallel library presentation
Task parallel library presentationahmed sayed
 
Async Await for Mobile Apps
Async Await for Mobile AppsAsync Await for Mobile Apps
Async Await for Mobile AppsCraig Dunn
 
End to-end async and await
End to-end async and awaitEnd to-end async and await
End to-end async and awaitvfabro
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingPraveen Prajapati
 
Async CTP 3 Presentation for MUGH 2012
Async CTP 3 Presentation for MUGH 2012Async CTP 3 Presentation for MUGH 2012
Async CTP 3 Presentation for MUGH 2012Sri Kanth
 
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
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and pythonChetan Giridhar
 
Ddd melbourne 2011 C# async ctp
Ddd melbourne 2011  C# async ctpDdd melbourne 2011  C# async ctp
Ddd melbourne 2011 C# async ctpPratik Khasnabis
 
Asynchronous programming in ASP.NET
Asynchronous programming in ASP.NETAsynchronous programming in ASP.NET
Asynchronous programming in ASP.NETAlex Thissen
 
Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)
Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)
Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)Panagiotis Kanavos
 
PyCon Canada 2019 - Introduction to Asynchronous Programming
PyCon Canada 2019 - Introduction to Asynchronous ProgrammingPyCon Canada 2019 - Introduction to Asynchronous Programming
PyCon Canada 2019 - Introduction to Asynchronous ProgrammingJuti Noppornpitak
 
Asynchronous Programming.pptx
Asynchronous Programming.pptxAsynchronous Programming.pptx
Asynchronous Programming.pptxAayush Chimaniya
 
Asynchronous Programming in .NET
Asynchronous Programming in .NETAsynchronous Programming in .NET
Asynchronous Programming in .NETPierre-Luc Maheu
 
Asynchronous programming - .NET Way
Asynchronous programming - .NET WayAsynchronous programming - .NET Way
Asynchronous programming - .NET WayBishnu Rawal
 
Sync with async
Sync with  asyncSync with  async
Sync with asyncprabathsl
 
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)
Parallel and Asynchronous Programming -  ITProDevConnections 2012 (Greek)Parallel and Asynchronous Programming -  ITProDevConnections 2012 (Greek)
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)Panagiotis Kanavos
 
C# Parallel programming
C# Parallel programmingC# Parallel programming
C# Parallel programmingUmeshwaran V
 
Ratpack Web Framework
Ratpack Web FrameworkRatpack Web Framework
Ratpack Web FrameworkDaniel Woods
 

Similar to Training – Going Async (20)

Task parallel library presentation
Task parallel library presentationTask parallel library presentation
Task parallel library presentation
 
Async Await for Mobile Apps
Async Await for Mobile AppsAsync Await for Mobile Apps
Async Await for Mobile Apps
 
End to-end async and await
End to-end async and awaitEnd to-end async and await
End to-end async and await
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programming
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
 
Async CTP 3 Presentation for MUGH 2012
Async CTP 3 Presentation for MUGH 2012Async CTP 3 Presentation for MUGH 2012
Async CTP 3 Presentation for MUGH 2012
 
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
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and python
 
Ddd melbourne 2011 C# async ctp
Ddd melbourne 2011  C# async ctpDdd melbourne 2011  C# async ctp
Ddd melbourne 2011 C# async ctp
 
Asynchronous programming in ASP.NET
Asynchronous programming in ASP.NETAsynchronous programming in ASP.NET
Asynchronous programming in ASP.NET
 
Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)
Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)Parallel and Asynchronous Programming -  ITProDevConnections 2012 (English)
Parallel and Asynchronous Programming - ITProDevConnections 2012 (English)
 
PyCon Canada 2019 - Introduction to Asynchronous Programming
PyCon Canada 2019 - Introduction to Asynchronous ProgrammingPyCon Canada 2019 - Introduction to Asynchronous Programming
PyCon Canada 2019 - Introduction to Asynchronous Programming
 
Asynchronous Programming.pptx
Asynchronous Programming.pptxAsynchronous Programming.pptx
Asynchronous Programming.pptx
 
Salesforce asynchronous apex
Salesforce asynchronous apexSalesforce asynchronous apex
Salesforce asynchronous apex
 
Asynchronous Programming in .NET
Asynchronous Programming in .NETAsynchronous Programming in .NET
Asynchronous Programming in .NET
 
Asynchronous programming - .NET Way
Asynchronous programming - .NET WayAsynchronous programming - .NET Way
Asynchronous programming - .NET Way
 
Sync with async
Sync with  asyncSync with  async
Sync with async
 
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)
Parallel and Asynchronous Programming -  ITProDevConnections 2012 (Greek)Parallel and Asynchronous Programming -  ITProDevConnections 2012 (Greek)
Parallel and Asynchronous Programming - ITProDevConnections 2012 (Greek)
 
C# Parallel programming
C# Parallel programmingC# Parallel programming
C# Parallel programming
 
Ratpack Web Framework
Ratpack Web FrameworkRatpack Web Framework
Ratpack Web Framework
 

More from Betclic Everest Group Tech Team

Mini-training: Personalization & Recommendation Demystified
Mini-training: Personalization & Recommendation DemystifiedMini-training: Personalization & Recommendation Demystified
Mini-training: Personalization & Recommendation DemystifiedBetclic Everest Group Tech Team
 

More from Betclic Everest Group Tech Team (20)

Mini training - Moving to xUnit.net
Mini training - Moving to xUnit.netMini training - Moving to xUnit.net
Mini training - Moving to xUnit.net
 
Mini training - Introduction to Microsoft Azure Storage
Mini training - Introduction to Microsoft Azure StorageMini training - Introduction to Microsoft Azure Storage
Mini training - Introduction to Microsoft Azure Storage
 
Akka.Net
Akka.NetAkka.Net
Akka.Net
 
Mini training- Scenario Driven Design
Mini training- Scenario Driven DesignMini training- Scenario Driven Design
Mini training- Scenario Driven Design
 
Email Management in Outlook
Email Management in OutlookEmail Management in Outlook
Email Management in Outlook
 
Mini-Training: SSO with Windows Identity Foundation
Mini-Training: SSO with Windows Identity FoundationMini-Training: SSO with Windows Identity Foundation
Mini-Training: SSO with Windows Identity Foundation
 
Mini-Training: Docker
Mini-Training: DockerMini-Training: Docker
Mini-Training: Docker
 
Mini Training Flyway
Mini Training FlywayMini Training Flyway
Mini Training Flyway
 
Mini-Training: NDepend
Mini-Training: NDependMini-Training: NDepend
Mini-Training: NDepend
 
Management 3.0 Workout
Management 3.0 WorkoutManagement 3.0 Workout
Management 3.0 Workout
 
Lean for Business
Lean for BusinessLean for Business
Lean for Business
 
Short-Training asp.net vNext
Short-Training asp.net vNextShort-Training asp.net vNext
Short-Training asp.net vNext
 
Mini-Training: Mobile UX Trends
Mini-Training: Mobile UX TrendsMini-Training: Mobile UX Trends
Mini-Training: Mobile UX Trends
 
Training: MVVM Pattern
Training: MVVM PatternTraining: MVVM Pattern
Training: MVVM Pattern
 
Mini-training: Personalization & Recommendation Demystified
Mini-training: Personalization & Recommendation DemystifiedMini-training: Personalization & Recommendation Demystified
Mini-training: Personalization & Recommendation Demystified
 
Mini-training: Let’s Git It!
Mini-training: Let’s Git It!Mini-training: Let’s Git It!
Mini-training: Let’s Git It!
 
AngularJS Best Practices
AngularJS Best PracticesAngularJS Best Practices
AngularJS Best Practices
 
Mini-Training: Roslyn
Mini-Training: RoslynMini-Training: Roslyn
Mini-Training: Roslyn
 
Mini-Training: Netflix Simian Army
Mini-Training: Netflix Simian ArmyMini-Training: Netflix Simian Army
Mini-Training: Netflix Simian Army
 
WCF Configuration - The basics
WCF Configuration - The basicsWCF Configuration - The basics
WCF Configuration - The basics
 

Recently uploaded

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxbodapatigopi8531
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 

Recently uploaded (20)

Hand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptxHand gesture recognition PROJECT PPT.pptx
Hand gesture recognition PROJECT PPT.pptx
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 

Training – Going Async

  • 1. GOING ASYNC THERE IS NO THREAT MAXIME LEMAITRE – 29/10/14
  • 2. Agenda • Introduction • Brief History • Refresher of the TPL • What’s different since 4.5 ? • Best Practices • Working with Async Asynchronous programming is a means of parallel programming in which a unit of work runs separately from the main application thread and notifies the calling thread of its completion, failure or progress. Why do we need it now ?
  • 3. Dangers of Synchronous programming Client-Side Server-Side
  • 4. What does asynchrony really mean? Synchronous Perform something here and now. I’ll regain control to execute something else when it’s done. Asynchronous Initiate something here and now. I’ll regain control to execute something else “immediately”. How an operation is invoked implies nothing about the implementation of the workload itself. Async Clinic
  • 5. Sync vs Async “Pause for 10 seconds, then output 'Hello' to the console.” Synchronous Asynchronous Both “async over sync” and “sync over async” can be problematic if you don’t completely understand the implementation.
  • 7. Going Async public class MyClass { public int Read(byte [] buffer, int offset, int count); } public class MyClass { public Task<int> ReadAsync(byte [] buffer, int offset, int count); } 10 years 1.0 (2002) 4.5 (2012)
  • 8. Asynchronous Programming Model (APM) http://msdn.microsoft.com/en-us/library/ms228963.aspx • Introduced in .net 1.1 (no async support before) • Asynchronous operations require BeginXX and EndXX methods – Example : BeginWrite and EndWrite for asynchronous write operations). • No longer recommended • Several APIs and legacy code still use this pattern public class MyClass { public IAsyncResult BeginRead( byte [] buffer, int offset, int count, AsyncCallback callback, object state); public int EndRead(IAsyncResult asyncResult); }
  • 9. Event-based Asynchronous Pattern (EAP) http://msdn.microsoft.com/en-us/library/ms228969.aspx • Introduced in .net 2.0 • Requires a method that has the Async suffix, and also requires one or more events, event handler delegate types, and EventArg-derived types. public class MyClass { public void ReadAsync(byte [] buffer, int offset, int count); public event ReadCompletedEventHandler ReadCompleted; } public delegate void ReadCompletedEventHandler( object sender, ReadCompletedEventArgs eventArgs); public class ReadCompletedEventArgs : AsyncCompletedEventArgs { public int Result { get; } }
  • 10. Task-based Asynchronous Pattern (TAP) http://msdn.microsoft.com/en-us/library/hh873175.aspx • Introduced in .net 4.0 but greatly improved in 4.5 • Uses a single method to represent the initiation and completion of an asynchronous operation. • Recommended approach to asynchronous programming in the .NET Framework. • Very similar to the synchronous version • Interop with Other Asynchronous Patterns and Types is supported (EAP<>TAP, APM<>TAP) public class MyClass { public Task<int> ReadAsync(byte [] buffer, int offset, int count); }
  • 11. Refresher on the TPL What is a Task/Task<> ?
  • 12. What is a Task/Task<> ? • A Task represents an asynchronous operation – resembles a thread or ThreadPool work item, but at a higher level of abstraction – Similar to promises in Javascript • Task can only run from start to finish once – you cannot run the same task object two times. Have to create another Task object for running the same code • More efficient and more scalable use of system resources. – Tasks are queued to the ThreadPool, which has been enhanced with algorithms that determine and adjust to the number of threads and that provide load balancing to maximize throughput. This makes tasks relatively lightweight, and you can create many of them to enable fine-grained parallelism. • More programmatic control than is possible with a thread or work item. – Rich set of APIs that support waiting, cancellation, continuations, robust exception handling, detailed status, custom scheduling, and more. • Task (as it is used by the TPL) is pretty much completely different than Task (as it is used by async). TPL is the preferred API for writing multi-threaded, asynchronous, and parallel code
  • 13. Task<T> Properties Task<T> has important properties. Don’t reinvent the wheel when consuming Task and Task<T> !
  • 14. Starting a Task • Threre are many ways to start a task. • Most of time, we don’t need to start a Task by yourself ; The API/Framework method will give a task. – eg HttpClient, StreamReader, … • If you still need to start a Task, use – Task.Run – Task.Factory.StartNew for a full control • About Task.Run – Do not use it just to “provide something awaitable” aka a fake-asynchronous method – CPU-bound tasks only Task.Run vs Task.Factory.StartNew
  • 15. Cancelling a Task • Cancellation is supported through the use of CancellationToken (via CancellationTokenSource ) to request cancellation • You can terminate an operation – By returning the delegate (In the case the status is RantoCompletion and not Canceled) – By throwing a OperationCanceledException via ThrowIfCancellationRequested • Cancellation token are present in all async API Task Cancellation
  • 16. Exceptions handling • User-code can throw intentionally (or not any) any kind of exceptions. • Exceptions are propagated back to the joining code when using Task.Wait(), Task.Result,await … – Be sure to always surround this with a try/catch statement • Task infrastructure wraps thrown exceptions in AggregateException instance. In that case, the task status is Faulted and Task.Exception contains Read more on TPL and Exceptions
  • 17. Task Continuations • A continuation task is an asynchronous task that is invoked by another task – Traditionally, this has been done by using callback methods • Continuations are relatively easy to use, but are nevertheless very powerful and flexible. For example, you can: – pass data from the antecedent to the continuation – specify the conditions under which the continuation will be invoked or not invoked – cancel a continuation either before it starts or cooperatively as it is running – invoke multiple continuations from the same antecedent – invoke one continuation when all or any one of multiple antecedents complete – chain continuations one after another to any arbitrary length – use a continuation to handle exceptions thrown by the antecedent – … • Many continuations options (NotOnFaulted, OnlyOnFaulted, ..) • Continuation Tasks is an important topic to achieve « Async all the way » Continuation Tasks
  • 20. What’s different now ? Asynchronous programming with .NET 4 is a little easier. Asynchronous programming with .NET 4.5 is a lot easier.
  • 21. Task = new Task TPL 4.5 Performance improvements
  • 22. TPL at the Core layer Application area Supporting APIs that contain async methods Web access HttpClient , SyndicationClient Working with files StorageFile, StreamWriter, StreamReader, XmlReader Working with images MediaCapture, BitmapEncoder, BitmapDecoder WCF programming Synchronous and Asynchronous Operations Asynchrony is essential for activities that are potentially blocking, ; so async programming was added in several APIs from the .NET Framework 4.5 and the Windows Runtime
  • 23. New Task Methods • Task.Run : Use it to offload work as a Task or Task<TResult> to the thread pool • Task.Delay : Use the Delay method to introduce pauses into an asynchronous method’s execution • Task.FromResult : Use the FromResult<TResult> method in scenarios where data may already be available and just needs to be returned from a task-returning method lifted into a Task<TResult>: • Task.WhenAll : Use the WhenAll method to asynchronously wait on multiple asynchronous operations that are represented as tasks • Task.WhenAny : Use the WhenAny method to asynchronously wait for just one of multiple asynchronous operations represented as tasks to complete • …
  • 24. New keywords : async/await added in .net 4.5 & C# 5 • Allow writing/debugging async code almost as if it is a usual synchronous code • Both keywords - async and await - always work together. – await without async is not allowed. – async without await is allowed, but not the method executes as a synchronous method Use the async modifier to specify that a method, lambda expression, or anonymous method is asynchronous. The async keyword was mainly added to avoid backwards compatibility problems when using the await keyword An async method can have a return type of Task, Task<TResult>, or void The await operator is applied to a task in an asynchronous method to suspend the execution of the method until the awaited task completes
  • 25. await/async control flow Control Flow in Async Programs
  • 26. Await/await with .net 4.0 • It’s possible to use async/await in projects targeting .net 4.0 • It does not include the ASP.NET runtime changes necessary for proper async/await support, so if you install it into an ASP.NET 4.0 project, the code will compile but will not work correctly • It’s better to upgade to 4.5
  • 27. There is no thread “if I am awaiting an operation, there must be a thread that is doing the wait! It’s probably a thread pool thread. Or an OS thread! Or something with a device driver…” There is no thread “Regardless of the type of I/O request, internally I/O operations issued to a driver on behalf of the application are performed asynchronously”, Windows Internals
  • 29. How do I know which method is async or not ? The .NET Framework 4.5 contains many members that work with async and await. You can recognize these members by the "Async" suffix that’s attached to the member name and a return type of Task or Task<TResult>. Library methods shouldn’t lie. Be honest. Use “XxAsync” if, and only if, you’re not thread-bound (with a few notable exceptions). Suffix should help caller to understand implementation.
  • 30. TAP Guidelines There are many new await-friendly techniques that should be used instead of the old blocking techniques. If you have any of these Old examples in your new async code, you’re Doing It Wrong. TAP Guidelines
  • 31. Async all the way • “await and async” is poison. – Await/await isn’t intended to be used in just a single function, but for an entire flow in your application. – Once you will add them somewhere in your code. These keywords will propagate all of the way up to the top of a logical stack. • Asynchronous code works best if asynchronous code calls and is called by other asynchronous code • Don’t mix synchronous and asynchronous code (without carefully considering the consequences) – bad idea to block on async code by calling Task.Wait or Task.Result. – Mixed async and blocking code can cause deadlocks, more-complex error handling and unexpected blocking of context threads • Exceptions – Console Applications, Unit Tests
  • 32. Asynchronous wrappers for synchronous methods? “async over sync” • Two primary benefits to asynchrony: scalability and offloading (e.g. responsiveness, parallelism) • Scalability – still consuming the same amount of resources (even a bit more) – Don’t put Task.Run everywhere to achieve async over sync. Use a thread from the thread pool. • Offloading – if a consumer of your library wants to use the synchronous method asynchronously, they can do so on their own Async http://blogs.msdn.com/b/pfxteam/archive/2012/03/24/10287244.aspx
  • 33. Synchronous wrappers for asynchronous methods? “sync over async” • Avoid exposing a synchronous method that just wraps the asynchronous implementation – hides from the consumer the true nature of the implementation – If the consumer chooses to block waiting for the asynchronous implementation to complete, that’s up to the caller • Can lead to significant problems with the application, such as hangs http://blogs.msdn.com/b/pfxteam/archive/2012/04/13/10293638.aspx
  • 34. Async with asp.net (API or MVC) What benefits ? Introduction to Async/Await on ASP.NET In the case of I/O bound operations (whether that's disk I/O or network I/O), the waiting that a thread might normally do is purposeless. Using an async version of the I/O method allows the worker thread -- which are generally in limited supply -- to go back into the worker pool to start servicing another request. When the I/O is ready, and a thread from the worker pool is available, then the original request can be completed. This can have a fairly substantial benefit in scalability of the server, because it means servicing lots more requests with far fewer resources. Don’t forget Parallelization : do not call sequentially N operations but parallelize work, and therefore return the data to the client sooner
  • 35. Tasks and asp.net MVC The way to build asynchronous controllers has been completely changed compared to how it was done previously with the AsyncController superclass. This class and the inherent complexity in using it are gone. Async
  • 36. Task in asp.net Web Api Mark your Api action with async + Task<Result> Message handlers also support async/await. (Cleaner than writing it with a continuation task).
  • 38. Async is for “long” tasks As there is a small overhead of using async (SynchronizationContext, Task Creation, Task Scheduling, Queueing…) ; so Do not put async everwhere. It won’t help. • Use where .NET 4.5 framework is Async – Network I/O – File I/O – Database I/O • CAUTION! (not all providers are async) – Remote Service Calls • Use for CPU-bound work – > 50ms with a user waiting – For server code, it depends
  • 39. Consider using await instead of ContinueWith/Unwrap • Async/await is better – Easier to read – Fewer context switches – Better memory footprint – especially with chained tasks – Less startup overhead TPL 4.5 Performance improvements
  • 40. Use cached Tasks when possible “While we have strived to slim down Task and eliminate unnecessary allocations in .NET 4.5, the optimal allocation is one that never happens. Sometimes it is possible to cache a single instance of a frequently used Task, eliminating the need to continually re-allocate the Task.”, Stephen Toub • Cache the Tasks, not the Data • reuse completed tasks • Use completed & static Tasks – Use Task.FromResult
  • 41. Encode closure information into a Task’s state object Compilation • Pass values by parameter where possible • Only capture the variable you are going to use. – Use local variables when necessary
  • 42. Don’t block on async code UI Client UI Asp.net Deadlock ! Why ? • After you await a Task, when the method continues it will continue in a context. • GUI and ASP.NET applications have a SynchronizationContext that permits only one chunk of code to run at a time Don’t block on async code
  • 43. Don't block on async code Call ConfigureAwait • Don’t Wait on Tasks – Let the runtime schedule the continuations • Don’t access Task.Result unless it’s completed – Task.Result is blocking • If you must, be aware of synchronized contexts: – UI Threads – ASP.NET Threads • For API developpers Don’t block on Tasks; use async all the way down – Call ConfigureAwait(false) It’s all about SynchronizationContext
  • 44. Use Async void for event Handlers void is a possible return type (with Task and Task<Result>) for an async method • Principles – Async void is a “fire-and-forget” mechanism... – The caller is unable to know when an async void has finished – The caller is unable to catch exceptions thrown from an async void • Guidance – Use async void methods only for top-level event handlers (and their like) – Use async Task-returning methods everywhere else – When you see an async lambda, verify it Async void Tips
  • 45. TPL Dataflow • TPL DataFlow is a library for building asynchronous data processing application • Not distributed with the .NET Framework 4.5, avaible via Nuget • A natural extension of the TPL library that allows developers to create data-processing pipelines in their applications • Many concepts of Actor model TPL Dataflow on MSDN
  • 46. Not Async Not Running Asynchronously. Why ? ‘async’ don’t forks. Use Task.Run to offload
  • 47. Method not completing Awaited task sometimes never completes. Why ? Always complete Tasks when the underlying operation has ended Deadlocking UI thread. Why ? Don’t synchronously wait on the UI thread. Use ConfigureAwait(false) whenever possible.
  • 49. References • http://www.codeproject.com/Articles/518856/Task-Parallel-Library-and-async-await-Functionalit • http://msdn.microsoft.com/en-us/library/dd997423(v=vs.110).aspx • http://www.asp.net/aspnet/overview/developing-apps-with-windows-azure/building-real-world-cloud-apps- with-windows-azure/web-development-best-practices#sessionstate • http://blog.stephencleary.com/ • http://msdn.microsoft.com/en-us/magazine/jj991977.aspx • http://msdn.microsoft.com/fr-fr/magazine/dn802603(en-us).aspx • http://bradwilson.typepad.com/blog/2012/04/tpl-and-servers-pt1.html • http://download.microsoft.com/download/5/B/9/5B924336-AA5D-4903-95A0-56C6336E32C9/TAP.docx • http://blogs.msdn.com/b/dotnet/archive/2012/04/03/async-in-4-5-worth-the-await.aspx • http://msdn.microsoft.com/en-us/magazine/gg598924.aspx • http://msdn.microsoft.com/en-us/library/hh191443.aspx • http://www.codeproject.com/Articles/562021/Asynchronous-models-and-patterns#await-async • http://www.codeproject.com/Articles/518856/Task-Parallel-Library-and-async-await-Functionalit • http://blogs.msdn.com/b/pfxteam/archive/2012/04/12/10293335.aspx • http://msdn.microsoft.com/en-us/library/hh873173.aspx • http://www.microsoft.com/en-us/download/confirmation.aspx?id=19957 • http://code.jonwagner.com/2012/09/06/best-practices-for-c-asyncawait/