please wait for the next slide clicking won’t make it come any faster

35
Async #1 – New In VS11 Lucian Wischik, Senior Program Manager (VB) Microsoft Corporation NDC 2012

Upload: amice-fleming

Post on 31-Dec-2015

221 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: please wait for the next slide clicking won’t make it come any faster

Async #1 – New In VS11Lucian Wischik, Senior Program Manager (VB)Microsoft Corporation

NDC 2012

Page 2: please wait for the next slide clicking won’t make it come any faster

A Language For Each Generation

Basic, C, AsmVB, C++, Delphi, C# AsyncFortran, Cobol

1960Mainframe

1980Microcomputer

1990Desktop

2010Distributed

Page 3: please wait for the next slide clicking won’t make it come any faster

A Language For Each Generation

Basic, C, AsmVB, C++, Delphi, C# AsyncFortran, Cobol

please wait for the next slideclicking won’t make it come any faster

Page 4: please wait for the next slide clicking won’t make it come any faster

Click

void LoadSettings() { IO.File.ReadAllText(path);}

void Button1_Click() { LoadSettings(); UpdateView();}

Click

Introducing AsyncM

ess

ag

e p

um

p

Page 5: please wait for the next slide clicking won’t make it come any faster

Click

void LoadSettings() { IO.Network.Download(path);}

void Button1_Click() { LoadSettings(); UpdateView();}

Click

Introducing AsyncM

ess

ag

e p

um

p

Page 6: please wait for the next slide clicking won’t make it come any faster

Plan of talk

1. Introduce asyncWhy make things async

How to make things async

Message pump

UI thread deadlock

Avoid re-entrancy

Async unit test

Multi-threaded sync context

3. Deep DiveHow it works

What next

2. Task Async PatternAsync Function FooAsync() As Task

Q & ATask.Run

Task.WhenAny

Task.WhenAll

Cancellation

Page 7: please wait for the next slide clicking won’t make it come any faster

DemoAdd the Async & Await keywords

Page 8: please wait for the next slide clicking won’t make it come any faster

async Task LoadSettingsAsync() { await IO.Network.DownloadAsync(path);}

void LoadSettings() { IO.Network.Download(path);}

async void Button1_Click() { await LoadSettingsAsync(); UpdateView();}

void Button1_Click(..) { LoadSettings(); UpdateView();}

Introducing AsyncM

ess

ag

e p

um

p

Click

Page 9: please wait for the next slide clicking won’t make it come any faster

Click

async Task LoadSettingsAsync() { await IO.Network.DownloadAsync(path);}

async void Button1_Click(){ await LoadSettingsAsync(); UpdateView();}

Click

Explaining AsyncM

ess

ag

e p

um

p

Task ...DownloadAsync

Task ...LoadSettingsAsync

Download

LoadSettings

Page 10: please wait for the next slide clicking won’t make it come any faster

DemoAsync UI app: re-entrancy and deadlockAsync unit testAsync console app

Page 11: please wait for the next slide clicking won’t make it come any faster

Introduce asyncWhy make things async

How to make things async

Message pump

Deadlock if block on UI thread

Avoid re-entrancy

The UI message-pump is the key to async

Photo: Rüdiger Wölk

Page 12: please wait for the next slide clicking won’t make it come any faster

Plan of talk

2. Task Async PatternAsync Function FooAsync() As Task

1. Introduce asyncWhy make things async

How to make things async

Message pump

UI thread deadlock

Avoid re-entrancy

Async unit test

Multi-threaded sync context

3. Deep DiveHow it works

What next

Q & ATask.Run

Task.WhenAny

Task.WhenAll

Cancellation

Page 13: please wait for the next slide clicking won’t make it come any faster

DemoUsing the Task type for compositional algorithmsCancellation

Page 14: please wait for the next slide clicking won’t make it come any faster

Task Async Pattern

Async Sub vs Async Function

Task.Run Task.WhenAny

Taks.WhenAll

Using Async

T.A.P.Function FooAsync() As Task.Only use Async Sub for top-level events.

Task type is a “promise”It represents a result that hasn’t yetbeen computed. A-syn-chronos.

Task is compositionalUse it to build sophisticated behavior.

Text/PicPictures can set a mood orevoke emotion, making fora more memorable presentation.

The task type is a “promise” of future results

Page 15: please wait for the next slide clicking won’t make it come any faster

Plan of talk

3. Deep DiveHow it works

What next

Q & A

2. Task Async PatternAsync Function FooAsync() As Task

1. Introduce asyncWhy make things async

How to make things async

Message pump

UI thread deadlock

Avoid re-entrancy

Async unit test

Multi-threaded sync context

Task.Run

Task.WhenAny

Task.WhenAll

Cancellation

Page 16: please wait for the next slide clicking won’t make it come any faster

This is the IL that’s emitted whenyou use async/await

Async: how it works

Page 17: please wait for the next slide clicking won’t make it come any faster

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

UIthread IOCP

thread

Async: How it works

Page 18: please wait for the next slide clicking won’t make it come any faster

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

UIthread IOCP

thread

Click

[1/12] A button-click arrives on the UI queue

Page 19: please wait for the next slide clicking won’t make it come any faster

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

UIthread IOCP

thread

Click

downTask

[2/12] Invoke some functions; get back “downTask” from the API

Page 20: please wait for the next slide clicking won’t make it come any faster

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

downTask

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

UIthread IOCP

thread

Click

downTask » sc.Post{Κ1}

[3/12] “Await downTask” assigns a continuation and returns loadTask

loadTask

Κ1:

Page 21: please wait for the next slide clicking won’t make it come any faster

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

loadTask

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

UIthread IOCP

thread

Click

downTask » sc.Post{Κ1}

[4/12] “Await loadTask” assigns a continuation and returns

Κ2:

loadTask » sc.Post{Κ2}

Κ1:

Page 22: please wait for the next slide clicking won’t make it come any faster

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

UIthread IOCP

thread

Click

conf

[5/12] Network packet arrives with data

downTask » sc.Post{Κ1}

Κ2:

loadTask » sc.Post{Κ2}

Κ1:

Page 23: please wait for the next slide clicking won’t make it come any faster

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

UIthread IOCP

thread

Click

confsc.Post{Κ1(conf)}

[6/12] Invoke downTask’s continuation with that data

downTask » sc.Post{Κ1}

Κ2:

loadTask » sc.Post{Κ2}

Κ1:

Page 24: please wait for the next slide clicking won’t make it come any faster

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

UIthread IOCP

thread

Click

loadTask » sc.Post{Κ2}

conf

K1(conf)

[7/12] Continuation is a “Post”, i.e. addition to the UI queue

Κ2:

sc.Post{Κ1(conf)}

Κ1:

Page 25: please wait for the next slide clicking won’t make it come any faster

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

UIthread IOCP

thread

Click

conf

K1(conf)

[8/12] UI thread executes K1

Κ2:

sc.Post{Κ1(conf)}

loadTask » sc.Post{Κ2}

Κ1:

Page 26: please wait for the next slide clicking won’t make it come any faster

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

UIthread IOCP

thread

Click

conf

K1(conf)

[9/12] Return statement will signal completion of loadTask

Κ2:

sc.Post{Κ1(conf)}

loadTask » sc.Post{Κ2}

Κ1:

Page 27: please wait for the next slide clicking won’t make it come any faster

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

UIthread IOCP

thread

Click

conf

K1(conf)

sc.Post(Κ2(len))

[10/12] Invoke loadTask’s continuation with data (by posting to UI queue)

K2(len) Κ2:

sc.Post{Κ1(rss)}

loadTask » sc.Post{Κ2}

Κ1:

Page 28: please wait for the next slide clicking won’t make it come any faster

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

UIthread IOCP

thread

Click

conf

K1(rss)

sc.Post(Κ2(len))

K2(len)

[11/12] Return from handling the K1 continuation

Κ2:

sc.Post{Κ1(conf)}

Κ1:

Page 29: please wait for the next slide clicking won’t make it come any faster

Async Task<int> LoadSettingsAsync() {

Button1.IsEnabled = False;

var downTask = IO.Network.DownloadAsync(path);

this.Config = await downTask;

return this.Config.Length;

}

async void Button1_Click() { var loadTask = LoadSettingsAsync();

var len = await loadTask;

UpdateView();}

conf

K2(len)

Click

K1(rss)

UIthread IOCP

thread

sc.Post(Κ2(len))

Κ1:

Κ2:

[12/12] UI thread executes K2

sc.Post{Κ1(conf)}

Page 30: please wait for the next slide clicking won’t make it come any faster

“A waiter’s job is to wait on a table until the

patrons have finished their

meal.

If you want to serve two

tables concurrently, you must hire two waiters.”

Page 31: please wait for the next slide clicking won’t make it come any faster

The following is from the Android dev blog.Can you spot the flaw?

1. “A good practice in creating responsive applications is to make sure your main UI thread does the minimum amount of work.”

2. “Any potentially long task that may hang your application should be handled in a different thread.”

3. “Typical examples of such tasks are network operations, which involve unpredictable delays.”

Page 32: please wait for the next slide clicking won’t make it come any faster

Asynchrony & concurrency don’t need multiple threads

Page 33: please wait for the next slide clicking won’t make it come any faster

Windows Phone…

.NET4Silverlight5Use VS2012 RC +Async Targeting Pack

.NET45Windows8Use VS2012 RC

T.A.P.From today onwards, all async library code you write should return Task.

What next to start using async

Page 34: please wait for the next slide clicking won’t make it come any faster

Q & A

Get VS2012 RChttp://msdn.microsoft.com/vstudio

Task Asynchronous Patternhttps://www.microsoft.com/download/en/details.aspx?id=19957http://www.wischik.com/lu/AsyncSilverlight/http://social.msdn.microsoft.com/Forums/async

Async Targeting Packhttp://msdn.microsoft.com/async

Downloads from this talkhttp://blogs.msdn.com/lucian

Page 35: please wait for the next slide clicking won’t make it come any faster

© 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.