joe hummel, phd microsoft mvp visual c++ technical staff: pluralsight, llc professor: u. of...

18
“Be More Responsive with Async and AwaitJoe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email: [email protected] stuff: http://www.joehummel.net/ downloads.html

Upload: liliana-day

Post on 03-Jan-2016

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

“Be More Responsive with Async and Await”

Joe Hummel, PhDMicrosoft MVP Visual C++

Technical Staff: Pluralsight, LLC

Professor: U. of Illinois, Chicago

email: [email protected]:http://www.joehummel.net/downloads.html

Page 2: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

2

To prevent blocking on long-running operations

◦ Client-side UI remains responsive vs. locking up

◦ Server-side scales vs. denying service

Why use Async / Await?

Page 3: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

3

Responsive UI…

Use-case #1

void button1_Click(…){ var result = DoLongLatencyOp(); lstBox.Items.Add(result);}

async void button1_Click(…) {

var result = await Task.Run(() => DoLongRunningOp());

lstBox.Items.Add(result); }

Page 4: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

4

Asian options financial modeling…

Demo ― Responsiveness

Page 5: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

5

Async / Await

async void button1_Click(…) {

var result = await Task.Run(() => DoLongRunningOp());

lstBox.Items.Add(result); }

Method *may* perform async, long-latency op

Tells compiler to setup a continuation that waits to execute rest of method on the current thread context. Meanwhile method can return while Task and continuation execute…

Page 6: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

6

await must wait on a task◦ implies underlying method must create & start a task…

Observation…

async void button1_Click(…) { var result = await Task.Run(() => DoLongRunningOp());

lstBox.Items.Add(result); }

System.IO.FileStream file = ...;

int read = await file.ReadAsync(buffer, offset, count);

Page 7: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

7

Think chunky, not chatty◦ i.e. designed for coarse-grain, long-latency operations

◦ file I/O, network I/O, compute-bound work…

Async / await is *not* for◦ Fire-and-forget (use a Task)

◦ High-performance parallelism (use Parallel.For)

Using Async / Await…

Page 8: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

8

Use with APIs exposing “TAP” pattern◦ Async / await take advantage of Task-based Asynchronous Pattern

Using Async / Await…

Synchronous Asynchronous

System.IO.FileStream Read ReadAsync

Write WriteAsync

CopyTo CopyToAsync

System.Net.HttpWebRequest GetResponse GetResponseAsync

GetRequestStream GetRequestStreamAsync

System.Net.Http.HttpClient N/A GetAsync

N/A PostAsync,PutAsync,SendAsync

Page 9: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

9

Scalable server-side…

Use-case #2

string GetData(string url){ var client = new WebClient(); var page = client.DownloadString(url); return page;}

async Task<string> GetDataAsync(string url) { var client = new WebClient();

var page = await client.DownloadStringTaskAsync(url);

return page; }

"asynchrony" bubbles up to caller…

Page 10: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

10

Asynchronous web requests…

Demo

Page 11: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

11

Return a task for caller to await upon…

Making your own TAP methods

public int SomeOperation(...){ int result;

result = ...;

return result;}

public Task<int> SomeOperationAsync(...){ return Task.Run<int>( () => { int result; result = ...; return result; } );}

Page 12: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

12

That’s it!

Page 13: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

13

Use cases:◦ Responsiveness: prevent blocking of the UI

◦ Scalability: prevent blocking of request-handling threads

Works with:◦ .NET on Windows server / desktop / tablet / phone

◦ Silverlight 4 and 5

◦ ASP.NET server-side

Summary: Async/Await

Page 14: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

14

Presenter: Joe Hummel◦ Email: [email protected]◦ Materials: http://www.joehummel.net/downloads.html

For more info, see these issues of MSDN magazine:

◦ October 2011: series of intro articles

◦ March 2013: best practices

◦ October 2014: async / await on ASP.NET

Thank you for attending!

Page 15: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

15

Programming model based on concept of a Task

Task-based

Task == a unit of work; an object denoting an ongoing operation or

computation.

Page 16: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

16

Asynchronous programming with Tasks

void button1_Click(…){ var uictx = // grab UI thread context to run UI task: TaskScheduler.FromCurrentSynchronizationContext();

Task.Factory.StartNew(()=> { return DoLongLatencyOp(); } ).ContinueWith((antecedent) => { lstBox.Items.Add(antecedent.Result); }, uictx // execute this task on UI thread: );}

void button1_Click(…){ var result = DoLongLatencyOp(); lstBox.Items.Add(result);}

Page 17: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

17

Task-based execution model

C C

C CC C

C C

Windows Process (.NET)

AppDomain

AppDomain

AppDomain

.NET Thread Pool

workerthread

workerthread

workerthread

workerthread

Parallel.For( ... );tasktasktasktask

global work queue

Task Parallel Library

Resource Manager

Task Scheduler

Windows

Page 18: Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago email:joe@joehummel.net stuff:

18

Async vs. Parallel?

Async programming: Better

responsiveness…

GUIs (desktop, web, mobile)

Cloud

Windows 8

Parallel programming:

Better performance…

Engineering

Oil and Gas

Pharma

Science

Social media

C C

C CC C

C C

Disk and

network I/O number crunching and

big data processing