Joe Hummel, PhD Technical Staff: Pluralsight Adjunct Professor: UIC, LUC

Slides:



Advertisements
Similar presentations
Joe Hummel, PhD UC-Irvine
Advertisements

Pavel Yosifovich Hi-Tech College
Asynchronous Programming with C# and WinRT
Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Adjunct Professor: U. of Illinois, Chicago and Loyola University Chicago
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.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
Async Programming WITH ASYNC TASK
Virtual techdays INDIA │ 9-11 February 2011 Parallelism in.NET 4.0 Parag Paithankar │ Technology Advisor - Web, Microsoft India.
CS533 Concepts of Operating Systems Class 2 Thread vs Event-Based Programming.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
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.
Chapter 4: Threads Adapted to COP4610 by Robert van Engelen.
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:
Contact Information Office: 225 Neville Hall Office Hours: Monday and Wednesday 12:00-1:00 and by appointment.
Node.js - What is Node.js? -
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
please wait for the next slide clicking won’t make it come any faster.
Consuming REST Services from C# SoftUni Team Technical Trainers Software University
Procrastinator: Pacing Mobile Apps’ Usage of the Network mobisys 2014.
Parallel Programming: Responsiveness vs. Performance Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois,
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:
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.
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.
04 |Sharing Code Between Windows 8 and Windows Phone 8 in Visual Studio Ben Riga
1 Why Threads are a Bad Idea (for most purposes) based on a presentation by John Ousterhout Sun Microsystems Laboratories Threads!
Patterns of Parallel Programming with.NET 4 Stephen Toub Principal Architect Parallel Computing Platform Microsoft Corporation
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.
Lecturer 3: Processes multithreaded Operating System Concepts Process Concept Process Scheduling Operation on Processes Cooperating Processes Interprocess.
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.
Concepts of Multithreading CS 378 – Mobile Computing for iOS Dr. William C. Bulko.
Asynchronous Programming with C# v.Next
The Future of C# and Visual Basic
TechEd /6/2018 6:15 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Thread Fundamentals Header Advanced .NET Threading, Part 1
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
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.
Lecture Topics: 11/1 General Operating System Concepts Processes
Build /2/2019 © 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Hold up, wait a minute, let me put some async in it
03 | Async Programming & Networking Intro
Presentation transcript:

Joe Hummel, PhD Technical Staff: Pluralsight Adjunct Professor: UIC, LUC

 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 tasks number crunching and big data processing

4 C C C C C C C C Main thread C Main > if… while… Main > if… while… Work Stmt1; Stmt2; Stmt3; Work Stmt1; Stmt2; Stmt3; Main > if… while… Main > if… while… Work1 Stmt1; Stmt2; Stmt3; Work1 Stmt1; Stmt2; Stmt3; Work2 Stmt4; Stmt5; Stmt6; Work2 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); } Tells compiler that method *may* perform an async, long-latency op Tells compiler to execute operation but don’t wait. Instead, start op as a separate task, and setup a continuation to execute the remaining code when op finishes ― RUNNING ON THE SAME THREAD CONTEXT!

 For operations that may involve long latency ◦ File and network I/O are the classic use-case  For chunks of work you want to run in parallel ◦ And you have multi-core hardware 12

 Hides the complexities of async programming ◦ No visible callback, predictable exception handling, …  Think chunky, not chatty ◦ i.e. designed for coarse-grain work / long-latency operations  Designed to be used with APM pattern ◦ Asynchronous Programming Model  APIs that offer async calls via APM? ◦ File I/O ◦ Network I/O ◦ Windows 8 API 13

 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

 Asynchronous web requests… ◦ Work bottom-up changing sync calls to async calls ◦ Add await, async, and Task or Task as needed 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

16

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

 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” 18