More Related Content
Similar to Asynchronous Programming (20)
More from LearnNowOnline (6)
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
- 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
- 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
- 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
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n
- \n