please wait for the next slide clicking won’t make it come any faster
Post on 31-Dec-2015
221 Views
Preview:
TRANSCRIPT
Async #1 – New In VS11Lucian Wischik, Senior Program Manager (VB)Microsoft Corporation
NDC 2012
A Language For Each Generation
Basic, C, AsmVB, C++, Delphi, C# AsyncFortran, Cobol
1960Mainframe
1980Microcomputer
1990Desktop
2010Distributed
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
Click
void LoadSettings() { IO.File.ReadAllText(path);}
void Button1_Click() { LoadSettings(); UpdateView();}
Click
Introducing AsyncM
ess
ag
e p
um
p
Click
void LoadSettings() { IO.Network.Download(path);}
void Button1_Click() { LoadSettings(); UpdateView();}
Click
Introducing AsyncM
ess
ag
e p
um
p
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
DemoAdd the Async & Await keywords
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
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
DemoAsync UI app: re-entrancy and deadlockAsync unit testAsync console app
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
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
DemoUsing the Task type for compositional algorithmsCancellation
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
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
This is the IL that’s emitted whenyou use async/await
Async: how it works
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
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
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
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:
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:
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:
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:
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:
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:
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:
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:
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:
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)}
“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.”
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.”
Asynchrony & concurrency don’t need multiple threads
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
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
© 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.
top related