Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Adjunct Professor: U. of Illinois, Chicago and Loyola University Chicago

Slides:



Advertisements
Similar presentations
Joe Hummel, PhD UC-Irvine
Advertisements

DEV324 C# VB 7.0 Managed Code C# VB 8.0 Generics C# VB 9.0 Language Integrated Query C# VB 10.0 Dynamic + Language Parity.
Parallel Extensions to the.NET Framework Daniel Moth Microsoft
Pavel Yosifovich Hi-Tech College
Asynchronous programming Using Task, async and await Asynchronous programming1.
Cole Durdan.  What is asynchronous programming?  Previous patterns  Task Based Async Programming .NET 4.5 Keywords  What happens in an async. method?
Asynchronous programming Deadlock All The Things!.
{ async patterns } - or - using the asynchronous library in the.Net 4.5 Framework for more than keeping your UI responsive.
Developing for Windows 8/WinRT Session 4 Fundamentals Kevin Stumpf.
please wait for the next slide clicking won’t make it come any faster.
Async Programming WITH ASYNC TASK
Windows 8 Metro / HTML5 JS “First Blood” Peter Kellner Microsoft MVP, ASP.NET ASPInsider Primary Organizer Silicon Valley Code.
Virtual techdays INDIA │ 9-11 February 2011 Parallelism in.NET 4.0 Parag Paithankar │ Technology Advisor - Web, Microsoft India.
WHO WILL BENEFIT FROM THIS TALK TOPICS WHAT YOU’LL LEAVE WITH Web app developers who are already familiar with Windows Azure with scaling needs. Asynchronous.
Managed Code Generics Language Integrated Query Dynamic + Language Parity C# VB 11.0 Windows Runtime + Asynchrony C# VB 7.0 C# VB.
Joe Hummel, PhD Dept of Mathematics and Computer Science Lake Forest College Lecture 8: WebForms — Web-based.
App Windows UI object Windows object App code Windows object.
Lecture 8 – Platform as a Service. Introduction We have discussed the SPI model of Cloud Computing – IaaS – PaaS – SaaS.
Joe Hummel, PhD Visiting Researcher: U. of California, Irvine Adjunct Professor: U. of Illinois, Chicago & Loyola U., Chicago Materials:
Joe Hummel, PhD Technical Staff: Pluralsight Adjunct Professor: UIC, LUC
Joe Hummel, PhD Visiting Researcher: U. of California, Irvine Adjunct Professor: U. of Illinois, Chicago & Loyola U., Chicago Materials:
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Threads and Processes.
Parallel Extensions A glimpse into the parallel universe By Eric De Carufel Microsoft.NET Solution Architect at Orckestra
Consuming REST Services from C# SoftUni Team Technical Trainers Software University
Parallel Programming: Responsiveness vs. Performance Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois,
About Me Microsoft MVP Intel Blogger TechEd Israel, TechEd Europe Expert C++ Book
SIMPLE PARALLEL PROGRAMMING WITH PATTERNS AND OMNITHREADLIBRARY PRIMOŽ GABRIJELČIČ SKYPE: GABR42
public static void PausePrintAsync() { ThreadPool.QueueUserWorkItem(_ => PausePrint()); } public static Task PausePrintAsync() { return Task.Run(()
U NIVERSITY OF M ASSACHUSETTS A MHERST Department of Computer Science Computer Systems Principles Concurrency Patterns Emery Berger and Mark Corner University.
Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago stuff:
Joe Hummel, the compiler is at your service Chicago Code Camp 2014.
Future of VB and C# Lucian Wischik VB Language PM Microsoft.
WHO WILL BENEFIT FROM THIS TALK TOPICS WHAT YOU’LL LEAVE WITH ASP.NET developers, including Web Forms & MVC History of async programming in.NET How async.
Huseyin YILDIZ Software Design Engineer Microsoft Corporation SESSION CODE: DEV314.
Asynchronous Web Services Writing Asynchronous Web Services SoftUni Team Technical Trainers Software University
IAsyncResult ar = BeginSomething(…); // Do other work, checking ar.IsCompleted int result = EndSomething(ar);
Asynchronous Programming Writing Asynchronous Code in C# SoftUni Team Technical Trainers Software University
Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago stuff:
Module 8 Enhancing User Interface Responsiveness.
Joe Hummel, the compiler is at your service SDC Meetup, Sept 2014.
TAP into async programming
Other news? async and await Anonymous types (var, dynamic) Tuples Object instantiation Extension methods UCN Teknologi/act2learn1FEN 2014.
TOPICS WHAT YOU’LL LEAVE WITH WHO WILL BENEFIT FROM THIS TALK.NET library developers : with knowledge of async/await in C# / VB interested in low-level.
Silberschatz, Galvin and Gagne ©2013 Operating System Concepts – 9 th Edition Chapter 4: Threads.
Patterns of Parallel Programming with.NET 4 Stephen Toub Principal Architect Parallel Computing Platform Microsoft Corporation
Joe Hummel, PhD U. Of Illinois, Chicago Loyola University Chicago
TOPICS WHAT YOU’LL LEAVE WITH WHO WILL BENEFIT FROM THIS TALK.NET developers: familiar with parallel programming support in Visual Studio 2010 and.NET.
C# Present and Future Marita Paletsou Software Engineer.
C# 5.0 Alex Davies 22 nd December What we will cover C# 5.0,.NET 4.5, Visual Studio 11 Caller Info Attributes Upgrade from synchronous to asynchronous.
Joe Hummel, the compiler is at your service Chicago Coder Conference, June 2016.
Async Made Simple in Windows 8, with C# and Visual Basic Alex Turner Program Manager VB/C# Compilers Microsoft Corporation DEV332.
Asynchronous Programming Writing Concurrent Code in C# SoftUni Team Technical Trainers Software University
THE FUTURE OF C#: GOOD THINGS COME TO THOSE WHO ‘AWAIT’ Joseph Albahari SESSION CODE: DEV411 (c) 2011 Microsoft. All rights reserved.
Asynchronous Programming with C# v.Next
The Future of C# and Visual Basic
CS 341 : Programming Languages
TechEd /6/2018 6:15 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
CS399 New Beginnings Jonathan Walpole.
Async or Parallel? No they aren’t the same thing!
Chapter 4: Multithreaded Programming
Staying Afloat in the .NET Async Ocean
12 Asynchronous Programming
Creating Windows Store Apps Using Visual Basic
12/2/2018 4:10 AM © 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered.
Building responsive apps and sites with HTML5 web workers
TechEd /9/2018 4:17 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Build /2/2019 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Building Web Applications with Microsoft ASP
03 | Async Programming & Networking Intro
Presentation transcript:

Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Adjunct Professor: U. of Illinois, Chicago and Loyola University Chicago stuff:

 Motivation  Execution model  Parallel programming with Tasks  Parallel programming with Async / Await  Demos 2

3  Async programming:  Better responsiveness…  GUIs (desktop, web, mobile)  Cloud  Windows 8  Parallel programming:  Better performance…  Engineering  Oil and Gas  Pharma  Science  Social media Disk and network I/O number crunching and big data processing

4 C C C C C C C C Main thread C Main > if… while… Main > if… while… Task Stmt1; Stmt2; Stmt3; Task Stmt1; Stmt2; Stmt3; Main > if… while… Main > if… while… Task1 Stmt1; Stmt2; Stmt3; Task1 Stmt1; Stmt2; Stmt3; Task2 Stmt4; Stmt5; Stmt6; Task2 Stmt4; Stmt5; Stmt6; Worker thread Main thread Threads share, run asynchr onously Threads run in parallel  Single core:  Multicore:

 Threads(.NET 1.0)  Async Delegates  QueueUserWorkItem  BackgroundWorker  Task Parallel Library(.NET 4.0)  Async / Await(.NET 4.5) 5 Easier…

 Mandelbrot set… 6

 Programming model based on concept of a Task 7 Task == a unit of work; an object denoting an ongoing operation or computation.

8 Windows Process (.NET) App Domain App Domain App Domain App Domain App Domain App Domain.NET Thread Pool worker thread worker thread worker thread worker thread Parallel.For(... ); task global work queue Task Parallel Library Resource Manager Task Scheduler Windows

 Asian options financial modeling… 9

10 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 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); } void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); }

11 void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } void button1_Click(…) { var result = DoLongLatencyOp(); lstBox.Items.Add(result); } async void button1_Click(…) { var result = await Task.Run(() => DoLongRunningOp()); lstBox.Items.Add(result); } 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 to execute rest of method --- ON SAME THREAD CONTEXT --- and then returns from method so it doesn’t wait.

 await must wait on a task ◦ implies underlying method must create & start a task… 12 async void button1_Click(…) { var result = await Task.Run(() => DoLongRunningOp()); lstBox.Items.Add(result); } 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); System.IO.FileStream file =...; int read = await file.ReadAsync(buffer, offset, count);

 Think chunky, not chatty ◦ i.e. designed for coarse-grain, long-latency operations ◦ file I/O, network I/O, compute-bound work…  Think APIs exposing “TAP” pattern ◦ designed to take advantage of Task-based Asynchronous Pattern

 Async web calls are a classic use-case 14 private byte[] GetURLContents(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = webReq.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { responseStream.CopyTo(content); } return content.ToArray(); } private byte[] GetURLContents(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = webReq.GetResponse()) { using (Stream responseStream = response.GetResponseStream()) { responseStream.CopyTo(content); } return content.ToArray(); } Synchronous Version data = GetURLContents(“...”); // call and prepare to wait:

15 private async Task GetURLContentsAsync(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = await webReq.GetResponseAsync()) { using (Stream responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(content); } return content.ToArray(); } private async Task GetURLContentsAsync(string url) { var content = new MemoryStream(); var webReq = (HttpWebRequest)WebRequest.Create(url); using (WebResponse response = await webReq.GetResponseAsync()) { using (Stream responseStream = response.GetResponseStream()) { await responseStream.CopyToAsync(content); } } return content.ToArray(); } Asynchronous Version data = await GetURLContentsAsync(“...”); // call, start, don’t wait: Synchronous Version

 Asynchronous web requests… 16

 Return a task for caller to await upon… 17 public int SomeOperation(...) { int result; result =...; return result; } public int SomeOperation(...) { int result; result =...; return result; } public Task SomeOperationAsync(...) { return Task.Run ( () => { int result; result =...; return result; } ); } public Task SomeOperationAsync(...) { return Task.Run ( () => { int result; result =...; return result; } ); }

18

 Thread-based execution model at the bottom  Task-based execution model on top  For Performance: ◦ Prefer Task Parallel Library  For Responsiveness: ◦ Prefer Async / Await 19

 Presenter: Joe Hummel ◦ ◦ Materials:  For more info: ◦ MSDN Magazine, October 2011 (3 articles): 1.“Easier Asynchronous Programming with the New Visual Studio Async CTP” 2.“Pause and Play with Await” 3.“Async Performance: Understanding the Costs of Async and Await” 20