Asynchronous Programming

Page 1

Asynchronous Programming http://www.LearnNowOnline.com

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


Understanding the Problem with Previous Async Techniques • Demo

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 resources Learn More @ http://www.learnnowonline.com Copyright © by Application Developers Training Company


Previous Async Technologies • Asynchronous Programming Model (APM) • Relies on delegates to handle execution and

callback • Complex

• Event-based Asynchronous Pattern (EAP) • Simpler and more features than APM • Still not as productive as new Async

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 Method • 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 • No ref or out parameters 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 • Demo

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 • 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 • 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 Progress Reporting • Demo

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 • 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 • 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 • Callback handles progress display

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


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.