Asynchronous Programming

804 views

Published on

All about Asynchronous Programming: where it fits in, using the new async.

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
804
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
20
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \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
  • Asynchronous Programming

    1. 1. AsynchronousProgramming Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    2. 2. AsynchronousProgramming http:// www.LearnNowOnline.com Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    3. 3. Understanding the Problemwith Previous Async Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    4. 4. Understanding the Problemwith Previous Async• Demo Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    5. 5. Where Async Fits In Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    6. 6. Where Async Fits In• Traditional code is synchronous Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    7. 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. 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. 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. 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. 11. Previous Async Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    12. 12. Previous Async• Asynchronous Programming Model (APM) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    13. 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. 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. 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. 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. 17. Using the New asyncand await Keywords Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    18. 18. Using the New asyncand await Keywords• Demo Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    19. 19. Anatomy of an Async Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    20. 20. Anatomy of an Async• Modify method as async (required) Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    21. 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. 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. 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. 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. 25. Async Return Values Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    26. 26. Async Return Values• Can be void, Task, or Task<TResult> Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    27. 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. 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. 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. 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. 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. 32. Managing Async Tasks Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    33. 33. Managing Async Tasks• Demo Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    34. 34. About Async Threads Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    35. 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. 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. 37. Awaiting in Sequence Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    38. 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. 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. 40. Awaiting in Parallel Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    41. 41. Awaiting in Parallel• You can await multiple methods together Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    42. 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. 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. 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. 45. Exceptions,Cancellations, and Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    46. 46. Exceptions,Cancellations, and• Demo Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    47. 47. Handling Exceptions Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    48. 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. 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. 50. Cancellation Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    51. 51. Cancellation• Use CancellationTokenSource Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    52. 52. Cancellation• Use CancellationTokenSource• Pass Token to async methods Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    53. 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. 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. 55. Progress Reporting Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    56. 56. Progress Reporting• Modify async method to accept an IProgress<T> Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    57. 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. 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. 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. 60. Questions? Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company
    61. 61. Questions? http:// www.LearnNowOnline.com Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company

    ×