17
Easy Asynchrony with C#: No More Callbacks! Mads Torgersen Program Manager 3-011

var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

Embed Size (px)

Citation preview

Page 1: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

Easy Asynchrony with C#: No More Callbacks!

Mads TorgersenProgram Manager3-011

Page 2: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

The future is the future

Responsiveness and scalability

Async is becoming the norm

Futures: Modern abstractions

Details: Different but similar

Windows Runtime

Async

.NET Asyn

c

Page 3: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

Synchronous versus asynchronous

var data = DownloadData(...);ProcessData(data);

var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

DownloadDataAsync ProcessData

STOP

ProcessDataDownloadData

Page 4: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

Synchronous versus asynchronous

var data = DownloadData(...);ProcessData(data);

DownloadDataAsync ProcessData

STOP

ProcessDataDownloadData

STOP

var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

Page 5: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

C# and Visual Basic let you doasynchronous programmingwithout callbacks

Page 6: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

demo

Using await with the Windows Runtime

Page 7: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

asyncmakes your method asynchronous

Page 8: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

await makes the rest of your method a callback

Page 9: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

Awaitables

TaskTask<TResult>

Returned from asyncC# and Visual Basic APIs

Already running

await directlyCan await multiple timesStore and await later

IAsyncActionIAsyncOperation<TResult>

Returned from asyncWindows Runtime APIs

Already running

await directlyCan await multiple timesStore and await laterAsTask to get a Task

Page 10: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

Task-returning verus void-returning

async Task FooAsync(…);

Can be awaited“Give back control”Delegate asynchronous work

Use for helper methodsUse for library methods

async void Foo_Click(…);

Cannot be awaited“Fire and forget” Start separate independent flow

Use for event handlersUse to override void methods

Page 11: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

demo

Coordinating tasks

Page 12: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

Task lets you coordinate activities

Page 13: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

Task helpers

• Yielding controlawait Task.Delay(5000);await Task.Yield();

• Background runningvar result = await Task.Run(() => { … work … });

• Parallel compositionTask first = await Task.WhenAny(task1, task2);var results = await Task.WhenAll(task1, task2);

Page 14: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

demo

Cancellation

Page 15: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

Cancellation and Progress

Pass CancellationToken to send cancellationPass IProgress to receive progress updates

await FooAsync(…, cancel, progress);await FooAsync(…).AsTask(cancel, progress);

Page 16: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

No more callbacks!

Page 17: var data = DownloadData(...); ProcessData(data); var future = DownloadDataAsync(...); future.ContinueWith(data => ProcessData(data));

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.