SlideShare a Scribd company logo
Asynchronous
Programming




    Learn More @ http://www.learnnowonline.com
       Copyright © by Application Developers Training Company
Asynchronous
Programming
           http://
   www.LearnNowOnline.com



     Learn More @ http://www.learnnowonline.com
        Copyright © by Application Developers Training Company
Understanding the Problem
with Previous Async




       Learn More @ http://www.learnnowonline.com
          Copyright © by Application Developers Training Company
Understanding the Problem
with Previous Async
• Demo




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Where Async Fits In




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Where Async Fits In
• Traditional code is synchronous




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Where Async Fits In
• Traditional code is synchronous
  • Call method, block until call returns,
   continue processing




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Where Async Fits In
• Traditional code is synchronous
  • Call method, block until call returns,
   continue processing
• Asynchronous (async) is non-blocking




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Where Async Fits In
• Traditional code is synchronous
  • Call method, block until call returns,
   continue processing
• Asynchronous (async) is non-blocking
  • Call, return immediately to continue
   processing, called method continues
   running



          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Where Async Fits In
• Traditional code is synchronous
  • Call method, block until call returns,
   continue processing
• Asynchronous (async) is non-blocking
  • Call, return immediately to continue
   processing, called method continues
   running
• UI is responsive and good use of server

          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Previous Async




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Previous Async
• Asynchronous Programming Model
  (APM)




        Learn More @ http://www.learnnowonline.com
           Copyright © by Application Developers Training Company
Previous Async
• Asynchronous Programming Model
  (APM)
  • Relies on delegates to handle execution
   and callback




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Previous Async
• Asynchronous Programming Model
  (APM)
  • Relies on delegates to handle execution
    and callback
  • Complex




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Previous Async
• Asynchronous Programming Model
  (APM)
  • Relies on delegates to handle execution
    and callback
  • Complex
• Event-based Asynchronous Pattern
  (EAP)


          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Previous Async
• Asynchronous Programming Model
  (APM)
  • Relies on delegates to handle execution
    and callback
  • Complex
• Event-based Asynchronous Pattern
  (EAP)
  • Simpler and more features than APM


          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Using the New async
and await Keywords




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Using the New async
and await Keywords
• Demo




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Anatomy of an Async




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Anatomy of an Async
• Modify method as async (required)




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Anatomy of an Async
• Modify method as async (required)
• Call method with await (optional)




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Anatomy of an Async
• Modify method as async (required)
• Call method with await (optional)
• Without await: generates compiler
  warning and method runs
  synchronously




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Anatomy of an Async
• Modify method as async (required)
• Call method with await (optional)
• Without await: generates compiler
  warning and method runs
  synchronously
• Method, lambda, or anonymous
  method


         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Anatomy of an Async
• Modify method as async (required)
• Call method with await (optional)
• Without await: generates compiler
  warning and method runs
  synchronously
• Method, lambda, or anonymous
  method
• Add Async suffix to method name
         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Async Return Values




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Async Return Values
• Can be void, Task, or Task<TResult>




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Async Return Values
• Can be void, Task, or Task<TResult>
• Use void for fire-and-forget




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Async Return Values
• Can be void, Task, or Task<TResult>
• Use void for fire-and-forget
  • Warning: can’t catch exception with void




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Async Return Values
• Can be void, Task, or Task<TResult>
• Use void for fire-and-forget
  • Warning: can’t catch exception with void
  • Appropriate for async event handlers




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Async Return Values
• Can be void, Task, or Task<TResult>
• Use void for fire-and-forget
  • Warning: can’t catch exception with void
  • Appropriate for async event handlers
• Task and Task<TResult> lets callers
  await your method




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Async Return Values
• Can be void, Task, or Task<TResult>
• Use void for fire-and-forget
  • Warning: can’t catch exception with void
  • Appropriate for async event handlers
• Task and Task<TResult> lets callers
  await your method
• Assign awaited value to variable and
  continue with rest of method

          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Managing Async Tasks




     Learn More @ http://www.learnnowonline.com
        Copyright © by Application Developers Training Company
Managing Async Tasks
• Demo




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
About Async Threads




     Learn More @ http://www.learnnowonline.com
        Copyright © by Application Developers Training Company
About Async Threads
• Call to await returns to caller on same
  thread




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
About Async Threads
• Call to await returns to caller on same
  thread
• Control automatically marshaled back
  to UI thread after await




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Awaiting in Sequence




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Awaiting in Sequence
• You can have as many awaits in a
  method that you want




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Awaiting in Sequence
• You can have as many awaits in a
  method that you want
• After one await completes, the next
  one executes




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Awaiting in Parallel




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Awaiting in Parallel
• You can await multiple methods
  together




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Awaiting in Parallel
• You can await multiple methods
  together
• Add all methods to
  List<Task<TResult>>




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Awaiting in Parallel
• You can await multiple methods
  together
• Add all methods to
  List<Task<TResult>>
• Call await on Task.WhenAll to process
  results after all tasks complete



         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Awaiting in Parallel
• You can await multiple methods
  together
• Add all methods to
  List<Task<TResult>>
• Call await on Task.WhenAll to process
  results after all tasks complete
• Call await on Task.WhenAny to process
  results for each result as it completes
         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Exceptions,
Cancellations, and




       Learn More @ http://www.learnnowonline.com
          Copyright © by Application Developers Training Company
Exceptions,
Cancellations, and
• Demo




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Handling Exceptions




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Handling Exceptions
• You can handle both synchronous and
  async code together




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Handling Exceptions
• You can handle both synchronous and
  async code together
• Use try/catch around await and it
  works




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Cancellation




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Cancellation
• Use CancellationTokenSource




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Cancellation
• Use CancellationTokenSource
• Pass Token to async methods




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Cancellation
• Use CancellationTokenSource
• Pass Token to async methods
• Wrap in try/catch for
  OperationCancelledException




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Cancellation
• Use CancellationTokenSource
• Pass Token to async methods
• Wrap in try/catch for
  OperationCancelledException
• Awaited code can check token for
  cancellation and throw



         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Progress Reporting




      Learn More @ http://www.learnnowonline.com
         Copyright © by Application Developers Training Company
Progress Reporting
• Modify async method to accept an
  IProgress<T>




         Learn More @ http://www.learnnowonline.com
            Copyright © by Application Developers Training Company
Progress Reporting
• Modify async method to accept an
  IProgress<T>
• Caller instantiates ProgressReport with
  callback




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Progress Reporting
• Modify async method to accept an
  IProgress<T>
• Caller instantiates ProgressReport with
  callback
• Caller passes the ProgressReport instance
  to the async method




          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Progress Reporting
• Modify async method to accept an
  IProgress<T>
• Caller instantiates ProgressReport with
  callback
• Caller passes the ProgressReport instance
  to the async method
• The async method calls Report on
  IProgress<T> instance

          Learn More @ http://www.learnnowonline.com
             Copyright © by Application Developers Training Company
Questions?




     Learn More @ http://www.learnnowonline.com
        Copyright © by Application Developers Training Company
Questions?

           http://
   www.LearnNowOnline.com



     Learn More @ http://www.learnnowonline.com
        Copyright © by Application Developers Training Company

More Related Content

KEY
Web API HTTP Pipeline
PDF
Serverless in production, an experience report (London DevOps)
PDF
Frappé / ERPNext Open Day October 2015
KEY
Effective Testing with Ruby
PDF
Implementing Testing for Behavior-Driven Development Using Cucumber
PDF
Frappé / ERPNext Open Day November 2015
PPTX
Chicago Tech Day Jan 2015: Foundry - HTTP2
PDF
Edge 2014: Million Browser Botnet - Live Demonstration
Web API HTTP Pipeline
Serverless in production, an experience report (London DevOps)
Frappé / ERPNext Open Day October 2015
Effective Testing with Ruby
Implementing Testing for Behavior-Driven Development Using Cucumber
Frappé / ERPNext Open Day November 2015
Chicago Tech Day Jan 2015: Foundry - HTTP2
Edge 2014: Million Browser Botnet - Live Demonstration

What's hot (16)

PDF
Rafaëla Breed - Tracing performance of your service calls - Codemotion Amster...
PDF
Frappé / ERPNext Open Day February 2016
PPTX
Chicago Tech Day Jan 2015: Hidden Features
PDF
Edge 2014: Increasing Control with Property Manager with eBay
PDF
ERPNext Open Day - March / April 2015
PDF
Startup Pitching and Mobile App Startup
PDF
2015 Velocity SC: Convince your CFO that #perfmatters
PDF
SFScon18 - Juri Strumpflohner - End-to-end testing done right!
PDF
Frappe / ERPNext Open Day May 14
PPTX
Resolution for a Faster Site
PPTX
QuizBrahma by Sagar Kadam
PDF
Getting started with Visual Testing using Applitools - @TPC, Feb2020
PDF
Lambdaless and AWS CDK
PPTX
Yelowsoft delivers super app to bbr one of the biggest ride hailing companies...
PPTX
Yelowsoft delivers super app to bbr one of the biggest ride hailing companies...
PPTX
Automated Acceptance Test Practices and Pitfalls
Rafaëla Breed - Tracing performance of your service calls - Codemotion Amster...
Frappé / ERPNext Open Day February 2016
Chicago Tech Day Jan 2015: Hidden Features
Edge 2014: Increasing Control with Property Manager with eBay
ERPNext Open Day - March / April 2015
Startup Pitching and Mobile App Startup
2015 Velocity SC: Convince your CFO that #perfmatters
SFScon18 - Juri Strumpflohner - End-to-end testing done right!
Frappe / ERPNext Open Day May 14
Resolution for a Faster Site
QuizBrahma by Sagar Kadam
Getting started with Visual Testing using Applitools - @TPC, Feb2020
Lambdaless and AWS CDK
Yelowsoft delivers super app to bbr one of the biggest ride hailing companies...
Yelowsoft delivers super app to bbr one of the biggest ride hailing companies...
Automated Acceptance Test Practices and Pitfalls
Ad

Viewers also liked (6)

KEY
WPF: Working with Data
PPT
Windows 8: Shapes and Geometries
PPT
New in the Visual Studio 2012 IDE
KEY
WPF Binding
PPT
SQL: Permissions and Data Protection
KEY
Attributes, reflection, and dynamic programming
WPF: Working with Data
Windows 8: Shapes and Geometries
New in the Visual Studio 2012 IDE
WPF Binding
SQL: Permissions and Data Protection
Attributes, reflection, and dynamic programming
Ad

Similar to Asynchronous Programming (20)

PPTX
Async programming in c#
PDF
Webcast: Asynchronous Programming Demystified
PPTX
C# 5 deep drive into asynchronous programming
PDF
Using Async in your Mobile Apps - Marek Safar
PPTX
History of asynchronous in .NET
DOCX
Asynchronyin net
PPTX
End to-end async and await
PDF
Asynchronous Programming in C# and .NET Core - Edukite
PPTX
Async CTP 3 Presentation for MUGH 2012
PPTX
Windows Phone 8 - 3.5 Async Programming
PPTX
Training – Going Async
PPTX
Async in .NET
PPTX
Binary Studio Academy: Concurrency in C# 5.0
PPTX
MobConf - session on C# async-await on 18june2016 at Kochi
PPTX
CSharp 5 Async
PPTX
Task parallel library presentation
PDF
Async Await for Mobile Apps
PPSX
Async-await best practices in 10 minutes
PPTX
Async and Await on the Server
PDF
Async await...oh wait!
Async programming in c#
Webcast: Asynchronous Programming Demystified
C# 5 deep drive into asynchronous programming
Using Async in your Mobile Apps - Marek Safar
History of asynchronous in .NET
Asynchronyin net
End to-end async and await
Asynchronous Programming in C# and .NET Core - Edukite
Async CTP 3 Presentation for MUGH 2012
Windows Phone 8 - 3.5 Async Programming
Training – Going Async
Async in .NET
Binary Studio Academy: Concurrency in C# 5.0
MobConf - session on C# async-await on 18june2016 at Kochi
CSharp 5 Async
Task parallel library presentation
Async Await for Mobile Apps
Async-await best practices in 10 minutes
Async and Await on the Server
Async await...oh wait!

More from LearnNowOnline (20)

KEY
A tour of SQL Server
KEY
Introducing LINQ
KEY
Generics
KEY
Object oriented techniques
KEY
Object-Oriented JavaScript
KEY
SharePoint Document Management
KEY
SharePoint: Introduction to InfoPath
KEY
Managing site collections
KEY
Web API Basics
KEY
SQL Server: Security
KEY
Sql 2012 development and programming
KEY
What's new in Silverlight 5
KEY
KnockOutJS with ASP.NET MVC
KEY
Expression Blend Motion & Interaction Design
KEY
The Entity Data Model
KEY
Introducing the Entity Framework
KEY
Introduction to ASP.NET MVC
KEY
Working with Controllers and Actions in MVC
KEY
Creating a User Interface
KEY
Building Windows 8 Metro Style Applications Using JavaScript and HTML5
A tour of SQL Server
Introducing LINQ
Generics
Object oriented techniques
Object-Oriented JavaScript
SharePoint Document Management
SharePoint: Introduction to InfoPath
Managing site collections
Web API Basics
SQL Server: Security
Sql 2012 development and programming
What's new in Silverlight 5
KnockOutJS with ASP.NET MVC
Expression Blend Motion & Interaction Design
The Entity Data Model
Introducing the Entity Framework
Introduction to ASP.NET MVC
Working with Controllers and Actions in MVC
Creating a User Interface
Building Windows 8 Metro Style Applications Using JavaScript and HTML5

Asynchronous Programming

  • 1. Asynchronous Programming Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 2. Asynchronous Programming http:// www.LearnNowOnline.com Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 3. Understanding the Problem with Previous Async Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 4. Understanding the Problem with Previous Async • Demo Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 5. Where Async Fits In Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 6. Where Async Fits In • Traditional code is synchronous Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 7. Where Async Fits In • Traditional code is synchronous • Call method, block until call returns, continue processing Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 8. Where Async Fits In • Traditional code is synchronous • Call method, block until call returns, continue processing • Asynchronous (async) is non-blocking Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 9. Where Async Fits In • Traditional code is synchronous • Call method, block until call returns, continue processing • Asynchronous (async) is non-blocking • Call, return immediately to continue processing, called method continues running Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 10. Where Async Fits In • Traditional code is synchronous • Call method, block until call returns, continue processing • Asynchronous (async) is non-blocking • Call, return immediately to continue processing, called method continues running • UI is responsive and good use of server Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 11. Previous Async Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 12. Previous Async • Asynchronous Programming Model (APM) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 13. Previous Async • Asynchronous Programming Model (APM) • Relies on delegates to handle execution and callback Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 14. Previous Async • Asynchronous Programming Model (APM) • Relies on delegates to handle execution and callback • Complex Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 15. Previous Async • Asynchronous Programming Model (APM) • Relies on delegates to handle execution and callback • Complex • Event-based Asynchronous Pattern (EAP) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 16. Previous Async • Asynchronous Programming Model (APM) • Relies on delegates to handle execution and callback • Complex • Event-based Asynchronous Pattern (EAP) • Simpler and more features than APM Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 17. Using the New async and await Keywords Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 18. Using the New async and await Keywords • Demo Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 19. Anatomy of an Async Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 20. Anatomy of an Async • Modify method as async (required) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 21. Anatomy of an Async • Modify method as async (required) • Call method with await (optional) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 22. Anatomy of an Async • Modify method as async (required) • Call method with await (optional) • Without await: generates compiler warning and method runs synchronously Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 23. Anatomy of an Async • Modify method as async (required) • Call method with await (optional) • Without await: generates compiler warning and method runs synchronously • Method, lambda, or anonymous method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 24. Anatomy of an Async • Modify method as async (required) • Call method with await (optional) • Without await: generates compiler warning and method runs synchronously • Method, lambda, or anonymous method • Add Async suffix to method name Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 25. Async Return Values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 26. Async Return Values • Can be void, Task, or Task<TResult> Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 27. Async Return Values • Can be void, Task, or Task<TResult> • Use void for fire-and-forget Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 28. Async Return Values • Can be void, Task, or Task<TResult> • Use void for fire-and-forget • Warning: can’t catch exception with void Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 29. Async Return Values • Can be void, Task, or Task<TResult> • Use void for fire-and-forget • Warning: can’t catch exception with void • Appropriate for async event handlers Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 30. Async Return Values • Can be void, Task, or Task<TResult> • Use void for fire-and-forget • Warning: can’t catch exception with void • Appropriate for async event handlers • Task and Task<TResult> lets callers await your method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 31. Async Return Values • Can be void, Task, or Task<TResult> • Use void for fire-and-forget • Warning: can’t catch exception with void • Appropriate for async event handlers • Task and Task<TResult> lets callers await your method • Assign awaited value to variable and continue with rest of method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 32. Managing Async Tasks Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 33. Managing Async Tasks • Demo Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 34. About Async Threads Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 35. About Async Threads • Call to await returns to caller on same thread Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 36. About Async Threads • Call to await returns to caller on same thread • Control automatically marshaled back to UI thread after await Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 37. Awaiting in Sequence Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 38. Awaiting in Sequence • You can have as many awaits in a method that you want Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 39. Awaiting in Sequence • You can have as many awaits in a method that you want • After one await completes, the next one executes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 40. Awaiting in Parallel Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 41. Awaiting in Parallel • You can await multiple methods together Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 42. Awaiting in Parallel • You can await multiple methods together • Add all methods to List<Task<TResult>> Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 43. Awaiting in Parallel • You can await multiple methods together • Add all methods to List<Task<TResult>> • Call await on Task.WhenAll to process results after all tasks complete Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 44. Awaiting in Parallel • You can await multiple methods together • Add all methods to List<Task<TResult>> • Call await on Task.WhenAll to process results after all tasks complete • Call await on Task.WhenAny to process results for each result as it completes Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 45. Exceptions, Cancellations, and Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 46. Exceptions, Cancellations, and • Demo Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 47. Handling Exceptions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 48. Handling Exceptions • You can handle both synchronous and async code together Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 49. Handling Exceptions • You can handle both synchronous and async code together • Use try/catch around await and it works Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 50. Cancellation Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 51. Cancellation • Use CancellationTokenSource Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 52. Cancellation • Use CancellationTokenSource • Pass Token to async methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 53. Cancellation • Use CancellationTokenSource • Pass Token to async methods • Wrap in try/catch for OperationCancelledException Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 54. Cancellation • Use CancellationTokenSource • Pass Token to async methods • Wrap in try/catch for OperationCancelledException • Awaited code can check token for cancellation and throw Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 55. Progress Reporting Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 56. Progress Reporting • Modify async method to accept an IProgress<T> Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 57. Progress Reporting • Modify async method to accept an IProgress<T> • Caller instantiates ProgressReport with callback Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 58. Progress Reporting • Modify async method to accept an IProgress<T> • Caller instantiates ProgressReport with callback • Caller passes the ProgressReport instance to the async method Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 59. Progress Reporting • Modify async method to accept an IProgress<T> • Caller instantiates ProgressReport with callback • Caller passes the ProgressReport instance to the async method • The async method calls Report on IProgress<T> instance Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 60. Questions? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
  • 61. Questions? http:// www.LearnNowOnline.com Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company

Editor's Notes