KEMBAR78
Webcast: Asynchronous Programming Demystified | PDF
Asynchronous Programming
Demystified
1/14/2015
http://submain.com/webcasts/asynchronous-programming-demystified/
for the webcast recording, slides and demo code download
Webcast Housekeeping
1/14/2015
Audio
 Connect viaVoIP
 Plug in a headset or turn up your speakers
 Connect via Phone
 Select “UseTelephone” after joining the webinar
 Call 1 (415) 655-0053
Access Code: 805-361-561
Audio PIN: Shown after joining the webinar
Asking A Question
 Use the Questions window in the panel on the right of your screen
 Questions will be addressed at the end of the webcast
Recording
 A recording download link will be sent to all registrants within a few days
Copyright © SubMain 2015 2
Agenda
1/14/2015
 Overview of Async Patterns
 Introduction to async/await
 Asynchrony and Parallelism
 Benefits of async/await
 Demo
 Future Async webinars
 Q&A
Copyright © SubMain 2015 3
Introduction
Presenter
Stephen Cleary
Microsoft MVP
(g)host
Serge Baranovsky
Principal, SubMain
1/14/2015 Copyright © SubMain 2015 4
Stephen Cleary
stephencleary.com
C# Microsoft MVP
Concurrency in C# Cookbook (O’Reilly)
1/14/2015 Copyright © SubMain 2015 5
Introduction to async/await
1/14/2015 Copyright © SubMain 2015 6
Overview of Async Patterns
Async/await use theTask-basedAsync Pattern (TAP)
 Task SampleAsync();
Older: Event-based Async Pattern (EAP)
 void SampleAsync();
 event SampleCompleted;
Older: Async Programming Model (APM)
 IAsyncResult BeginSample();
 EndSample();
1/14/2015 Copyright © SubMain 2015 7
Syntax
Pair of keywords (async + await)
1/14/2015
int Test()
{
Thread.Sleep(100);
return 13;
}
async Task<int> TestAsync()
{
await Task.Delay(100);
return 13;
}
Copyright © SubMain 2015 8
What “async” means
Enables the await keyword in that method.
Creates a state machine for the method.
1/14/2015
async Task<int> TestAsync()
{
await Task.Delay(100);
return 13;
}
Copyright © SubMain 2015 9
What “await” means
An await expression takes one argument – an
“awaitable” (usually aTask orTask<T>).
 This “awaitable” represents an asynchronous operation.
1/14/2015
async Task<int> TestAsync()
{
await Task.Delay(100);
return 13;
}
Copyright © SubMain 2015 10
async Task<int> TestAsync()
{
var delayTask = Task.Delay(100);
await delayTask;
return 13;
}
What “await” means
Await will pause its async method until the operation
completes.
 If operation is already completed -> don’t pause.
 If operation faults -> raise exception.
Resume executing in a captured context by default.
1/14/2015
async Task<int> TestAsync()
{
await Task.Delay(100);
return 13;
}
Copyright © SubMain 2015 11
What “await” means
When await pauses:
 Returns an incomplete task to its caller.
When async method completes:
 Completes the task that was returned earlier.
The task represents the method.
1/14/2015
async Task<int> TestAsync()
{
await Task.Delay(100);
return 13;
}
Copyright © SubMain 2015 12
Captured Context
When await resumes:
 SynchronizationContext.Current orTaskScheduler.Current
What that means in practice:
 UI context, ASP.NET request context, or thread pool.
Avoiding context: use ConfigureAwait(false).
1/14/2015
async Task<int> TestAsync()
{
await Task.Delay(100);
return 13;
}
Copyright © SubMain 2015 13
What “await” doesn’t mean
Nothing to do with threads.
 Not at all like “asynchronous delegates”.
 Does not run the code on a background thread.
 Does not parallelize your code.
1/14/2015
async Task<int> TestAsync()
{
await Task.Delay(100);
return 13;
}
Copyright © SubMain 2015 14
Asynchrony != Parallelism
Each have their uses.
 Asynchrony – I/O, events.
 Parallelism – CPU-bound processing.
1/14/2015 Copyright © SubMain 2015 15
Concurrency
Asynchrony Parallelism
Benefits of async/await
 Responsiveness on the client side
 Scalability on the server side
 Naturally-asynchronous code has async APIs
 Naturally-synchronous code stays synchronous
 Asynchronous code is clean – no callbacks!
1/14/2015 Copyright © SubMain 2015 16
What is CodeIt.Right
1/14/2015 Copyright © SubMain 2015 17
What is CodeIt.Right
 Code Quality Analysis and Metrics
 Automated Code Review process
 Automated way to discover and
fix code smells
 Automatic and safe refactoring
of issues into conforming code
 Ensure your code adheres to
(your) predefined design
requirements and best coding
practices
1/14/2015 Copyright © SubMain 2015 18
What is CodeIt.Right - continued
Instant Code Review – real-time code checking
OnDemand Analysis
Source Control Check-In Policy
Build Process Integration
Hundreds of rules
 Security, Performance, Usage,
Design, Maintainability,
Exception Handling, Globalization,
Async, and more
1/14/2015 Copyright © SubMain 2015 19
Demo
Demo #1 – Correct async code
Demo #2 – Async naming convention
Demo #3 – Async void
Demo #4 – Blocking on async code
Demo #5 – Async code blocking
Demo #6 – Fake-async code
Demo #7 – Using ContinueWith
1/14/2015 Copyright © SubMain 2015 20
http://submain.com/webcasts/asynchronous-programming-demystified/
for the webcast recording, slides and demo code download
Asynchronous Programming
Async confusing? CodeIt.Right will guide
 CodeIt.Right Async rule set:
 Async method should have "Async" suffix
 Async method should have await statement
 Async method should returnTask orTask<T>
 Async method - avoid "out" and "ref" parameters
 Async method - await for completion
 Await statement - method should be async
 Async method - call Start on theTask
 Async method - do not useTask.Yield
 Async method - do not useTask.Wait
 Async method should not be Sub
 Async method parameters should be the same to synchronous counterpart
 Async method - transform to non-async if simple
1/14/2015 Copyright © SubMain 2015 22
Poll
 Future Async webinars
1/14/2015 Copyright © SubMain 2015 23
Q&A
Questions?
Email - customer-service@submain.com
Video - submain.com/codeit.right/video
Download the free CodeIt.Right trial at submain.com/codeit.right
Contact Stephen Cleary: stephencleary.com
1/14/2015
1 (800) 936-2134
Copyright © SubMain 2015 24
http://submain.com/webcasts/asynchronous-programming-demystified/
for the webcast recording, slides and demo code download

Webcast: Asynchronous Programming Demystified

  • 1.
  • 2.
    Webcast Housekeeping 1/14/2015 Audio  ConnectviaVoIP  Plug in a headset or turn up your speakers  Connect via Phone  Select “UseTelephone” after joining the webinar  Call 1 (415) 655-0053 Access Code: 805-361-561 Audio PIN: Shown after joining the webinar Asking A Question  Use the Questions window in the panel on the right of your screen  Questions will be addressed at the end of the webcast Recording  A recording download link will be sent to all registrants within a few days Copyright © SubMain 2015 2
  • 3.
    Agenda 1/14/2015  Overview ofAsync Patterns  Introduction to async/await  Asynchrony and Parallelism  Benefits of async/await  Demo  Future Async webinars  Q&A Copyright © SubMain 2015 3
  • 4.
    Introduction Presenter Stephen Cleary Microsoft MVP (g)host SergeBaranovsky Principal, SubMain 1/14/2015 Copyright © SubMain 2015 4
  • 5.
    Stephen Cleary stephencleary.com C# MicrosoftMVP Concurrency in C# Cookbook (O’Reilly) 1/14/2015 Copyright © SubMain 2015 5
  • 6.
    Introduction to async/await 1/14/2015Copyright © SubMain 2015 6
  • 7.
    Overview of AsyncPatterns Async/await use theTask-basedAsync Pattern (TAP)  Task SampleAsync(); Older: Event-based Async Pattern (EAP)  void SampleAsync();  event SampleCompleted; Older: Async Programming Model (APM)  IAsyncResult BeginSample();  EndSample(); 1/14/2015 Copyright © SubMain 2015 7
  • 8.
    Syntax Pair of keywords(async + await) 1/14/2015 int Test() { Thread.Sleep(100); return 13; } async Task<int> TestAsync() { await Task.Delay(100); return 13; } Copyright © SubMain 2015 8
  • 9.
    What “async” means Enablesthe await keyword in that method. Creates a state machine for the method. 1/14/2015 async Task<int> TestAsync() { await Task.Delay(100); return 13; } Copyright © SubMain 2015 9
  • 10.
    What “await” means Anawait expression takes one argument – an “awaitable” (usually aTask orTask<T>).  This “awaitable” represents an asynchronous operation. 1/14/2015 async Task<int> TestAsync() { await Task.Delay(100); return 13; } Copyright © SubMain 2015 10 async Task<int> TestAsync() { var delayTask = Task.Delay(100); await delayTask; return 13; }
  • 11.
    What “await” means Awaitwill pause its async method until the operation completes.  If operation is already completed -> don’t pause.  If operation faults -> raise exception. Resume executing in a captured context by default. 1/14/2015 async Task<int> TestAsync() { await Task.Delay(100); return 13; } Copyright © SubMain 2015 11
  • 12.
    What “await” means Whenawait pauses:  Returns an incomplete task to its caller. When async method completes:  Completes the task that was returned earlier. The task represents the method. 1/14/2015 async Task<int> TestAsync() { await Task.Delay(100); return 13; } Copyright © SubMain 2015 12
  • 13.
    Captured Context When awaitresumes:  SynchronizationContext.Current orTaskScheduler.Current What that means in practice:  UI context, ASP.NET request context, or thread pool. Avoiding context: use ConfigureAwait(false). 1/14/2015 async Task<int> TestAsync() { await Task.Delay(100); return 13; } Copyright © SubMain 2015 13
  • 14.
    What “await” doesn’tmean Nothing to do with threads.  Not at all like “asynchronous delegates”.  Does not run the code on a background thread.  Does not parallelize your code. 1/14/2015 async Task<int> TestAsync() { await Task.Delay(100); return 13; } Copyright © SubMain 2015 14
  • 15.
    Asynchrony != Parallelism Eachhave their uses.  Asynchrony – I/O, events.  Parallelism – CPU-bound processing. 1/14/2015 Copyright © SubMain 2015 15 Concurrency Asynchrony Parallelism
  • 16.
    Benefits of async/await Responsiveness on the client side  Scalability on the server side  Naturally-asynchronous code has async APIs  Naturally-synchronous code stays synchronous  Asynchronous code is clean – no callbacks! 1/14/2015 Copyright © SubMain 2015 16
  • 17.
    What is CodeIt.Right 1/14/2015Copyright © SubMain 2015 17
  • 18.
    What is CodeIt.Right Code Quality Analysis and Metrics  Automated Code Review process  Automated way to discover and fix code smells  Automatic and safe refactoring of issues into conforming code  Ensure your code adheres to (your) predefined design requirements and best coding practices 1/14/2015 Copyright © SubMain 2015 18
  • 19.
    What is CodeIt.Right- continued Instant Code Review – real-time code checking OnDemand Analysis Source Control Check-In Policy Build Process Integration Hundreds of rules  Security, Performance, Usage, Design, Maintainability, Exception Handling, Globalization, Async, and more 1/14/2015 Copyright © SubMain 2015 19
  • 20.
    Demo Demo #1 –Correct async code Demo #2 – Async naming convention Demo #3 – Async void Demo #4 – Blocking on async code Demo #5 – Async code blocking Demo #6 – Fake-async code Demo #7 – Using ContinueWith 1/14/2015 Copyright © SubMain 2015 20 http://submain.com/webcasts/asynchronous-programming-demystified/ for the webcast recording, slides and demo code download
  • 21.
    Asynchronous Programming Async confusing?CodeIt.Right will guide  CodeIt.Right Async rule set:  Async method should have "Async" suffix  Async method should have await statement  Async method should returnTask orTask<T>  Async method - avoid "out" and "ref" parameters  Async method - await for completion  Await statement - method should be async  Async method - call Start on theTask  Async method - do not useTask.Yield  Async method - do not useTask.Wait  Async method should not be Sub  Async method parameters should be the same to synchronous counterpart  Async method - transform to non-async if simple 1/14/2015 Copyright © SubMain 2015 22
  • 22.
    Poll  Future Asyncwebinars 1/14/2015 Copyright © SubMain 2015 23
  • 23.
    Q&A Questions? Email - customer-service@submain.com Video- submain.com/codeit.right/video Download the free CodeIt.Right trial at submain.com/codeit.right Contact Stephen Cleary: stephencleary.com 1/14/2015 1 (800) 936-2134 Copyright © SubMain 2015 24 http://submain.com/webcasts/asynchronous-programming-demystified/ for the webcast recording, slides and demo code download