Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "please wait for the next slide clicking won’t make it come any faster."— Presentation transcript:

1

2

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

4 Click void LoadSettings() { IO.File.ReadAllText(path); } void Button1_Click() { LoadSettings(); UpdateView(); } Click Message pump

5 Click void LoadSettings() { IO.Network.Download(path); } void Button1_Click() { LoadSettings(); UpdateView(); } Click Message pump

6

7 Demo Add the Async & Await keywords

8 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(); } Message pump Click

9 async Task LoadSettingsAsync() { await IO.Network.DownloadAsync(path); } async void Button1_Click(){ await LoadSettingsAsync(); UpdateView(); } Click Message pump Task... DownloadAsync Task... LoadSettingsAsync Download LoadSettings

10 Demo Async UI app: re-entrancy and deadlock Async unit test Async console app

11 Photo: Rüdiger Wölk

12

13 Demo Using the Task type for compositional algorithms Cancellation

14

15

16 This is the IL that’s emitted when you use async/await

17 Async Task 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(); } UI thread IOCP thread

18 async void Button1_Click() { var loadTask = LoadSettingsAsync(); var len = await loadTask; UpdateView(); } Async Task LoadSettingsAsync() { Button1.IsEnabled = False; var downTask = IO.Network.DownloadAsync(path); this.Config = await downTask; return this.Config.Length; } UI thread IOCP thread Click [1/12] A button-click arrives on the UI queue

19 async void Button1_Click() { var loadTask = LoadSettingsAsync(); var len = await loadTask; UpdateView(); } Async Task LoadSettingsAsync() { Button1.IsEnabled = False; var downTask = IO.Network.DownloadAsync(path); this.Config = await downTask; return this.Config.Length; } UI thread IOCP thread Click downTask [2/12] Invoke some functions; get back “downTask” from the API

20 Async Task 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(); } UI thread IOCP thread Click downTask » sc.Post{Κ1} [3/12] “Await downTask” assigns a continuation and returns loadTask loadTask Κ1:

21 async void Button1_Click() { var loadTask = LoadSettingsAsync(); var len = await loadTask; UpdateView(); } loadTask Async Task LoadSettingsAsync() { Button1.IsEnabled = False; var downTask = IO.Network.DownloadAsync(path); this.Config = await downTask; return this.Config.Length; } UI thread IOCP thread Click downTask » sc.Post{Κ1} [4/12] “Await loadTask” assigns a continuation and returns Κ2: loadTask » sc.Post{Κ2} Κ1:

22 async void Button1_Click() { var loadTask = LoadSettingsAsync(); var len = await loadTask; UpdateView(); } Async Task LoadSettingsAsync() { Button1.IsEnabled = False; var downTask = IO.Network.DownloadAsync(path); this.Config = await downTask; return this.Config.Length; } UI thread IOCP thread Click conf [5/12] Network packet arrives with data downTask » sc.Post{Κ1} Κ2: loadTask » sc.Post{Κ2} Κ1:

23 async void Button1_Click() { var loadTask = LoadSettingsAsync(); var len = await loadTask; UpdateView(); } Async Task LoadSettingsAsync() { Button1.IsEnabled = False; var downTask = IO.Network.DownloadAsync(path); this.Config = await downTask; return this.Config.Length; } UI thread IOCP thread Click conf sc.Post{Κ1(conf)} [6/12] Invoke downTask’s continuation with that data downTask » sc.Post{Κ1} Κ2: loadTask » sc.Post{Κ2} Κ1:

24 async void Button1_Click() { var loadTask = LoadSettingsAsync(); var len = await loadTask; UpdateView(); } Async Task LoadSettingsAsync() { Button1.IsEnabled = False; var downTask = IO.Network.DownloadAsync(path); this.Config = await downTask; return this.Config.Length; } UI thread 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:

25 async void Button1_Click() { var loadTask = LoadSettingsAsync(); var len = await loadTask; UpdateView(); } Async Task LoadSettingsAsync() { Button1.IsEnabled = False; var downTask = IO.Network.DownloadAsync(path); this.Config = await downTask; return this.Config.Length; } UI thread IOCP thread Click conf K1(conf) [8/12] UI thread executes K1 Κ2: sc.Post{Κ1(conf)} loadTask » sc.Post{Κ2} Κ1:

26 async void Button1_Click() { var loadTask = LoadSettingsAsync(); var len = await loadTask; UpdateView(); } Async Task LoadSettingsAsync() { Button1.IsEnabled = False; var downTask = IO.Network.DownloadAsync(path); this.Config = await downTask; return this.Config.Length; } UI thread 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:

27 async void Button1_Click() { var loadTask = LoadSettingsAsync(); var len = await loadTask; UpdateView(); } Async Task LoadSettingsAsync() { Button1.IsEnabled = False; var downTask = IO.Network.DownloadAsync(path); this.Config = await downTask; return this.Config.Length; } UI thread 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:

28 async void Button1_Click() { var loadTask = LoadSettingsAsync(); var len = await loadTask; UpdateView(); } Async Task LoadSettingsAsync() { Button1.IsEnabled = False; var downTask = IO.Network.DownloadAsync(path); this.Config = await downTask; return this.Config.Length; } UI thread 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:

29 Async Task 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) UI thread IOCP thread sc.Post(Κ2(len)) Κ1: Κ2: [12/12] UI thread executes K2 sc.Post{Κ1(conf)}

30 “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.”

31 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.”

32

33

34

35


Download ppt "please wait for the next slide clicking won’t make it come any faster."

Similar presentations


Ads by Google