Cole Durdan.  What is asynchronous programming?  Previous patterns  Task Based Async Programming .NET 4.5 Keywords  What happens in an async. method?

Slides:



Advertisements
Similar presentations
TechReady 16 4/1/2017 Async Clinic
Advertisements

Asynchronous I/O in .NET
ASP.NET Best Practices Dawit Wubshet Park University.
FUTURE OF.NET PARALLEL PROGRAMMING Joseph Albahari SESSION CODE: DEV308 (c) 2011 Microsoft. All rights reserved.
Patterns & practices Symposium 2013 Tips for building a Windows Store app using XAML and C#: The Kona project Francis Cheung
IAP C# Lecture 3 Parallel Computing Geza Kovacs. Sequential Execution So far, all our code has been executing instructions one after another, on a single.
CSE 1302 Lecture 21 Exception Handling and Parallel Programming Richard Gesick.
Andreas Ulbrich SDE – Microsoft Robotics.
Asynchronous programming Using Task, async and await Asynchronous programming1.
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!.
Stephen Toub Parallel Computing Platform Microsoft Corporation.
Async Programming WITH ASYNC TASK
Lecture 4 Thread Concepts. Thread Definition A thread is a lightweight process (LWP) which accesses a shared address space provided by by the parent process.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
CS533 Concepts of Operating Systems Class 2 The Duality of Threads and Events.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Multi-paradigm language Type inferred Just another.NET language Tools First-class language in VS2010* F# Interactive Window F# PowerPack F# = Fun.
Managed Code Generics Language Integrated Query Dynamic + Language Parity C# VB 11.0 Windows Runtime + Asynchrony C# VB 7.0 C# VB.
 George Chrysanthakopoulos Software Architect Microsoft Corporation.
Networking Nasrullah. Input stream Most clients will use input streams that read data from the file system (FileInputStream), the network (getInputStream()/getInputStream()),
Joe Hummel, PhD Technical Staff: Pluralsight Adjunct Professor: UIC, LUC
DEV301. // Synchronous TResult Foo(...); // Asynchronous Programming Model (APM) IAsyncResult BeginFoo(..., AsyncCallback callback, object state);
A Revolutionary Programming Pattern that Will Clean up your Code : Coroutines in C++ David Sackstein ACCU 2015.
Local, scheduled, periodic and push updates.
Consuming REST Services from C# SoftUni Team Technical Trainers Software University
Lecture 2 Foundations and Definitions Processes/Threads.
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(()
Joe Hummel, PhD Microsoft MVP Visual C++ Technical Staff: Pluralsight, LLC Professor: U. of Illinois, Chicago stuff:
Lecture 21 Parallel Programming Richard Gesick. Parallel Computing Parallel computing is a form of computation in which many operations are carried out.
ADVANCED WEB SERVICES. Three Advanced Web Service Techniques SOAP Extensions Asynchronous calls Custom wire formatting SOAP Extensions Asynchronous calls.
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.
CS333 Intro to Operating Systems Jonathan Walpole.
Laboratory - 4.  Threading Concept  Threading in.NET  Multi-Threaded Socket  Example.
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);
12/5/2015.net 1 system.net Contains any network functionallity you would need in c# Several sub namespaces exists to allow for more fined control –System.Net.Sockets.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
Asynchronous Programming Writing Asynchronous Code in C# SoftUni Team Technical Trainers Software University
TAP into async programming
Threads versus Events CSE451 Andrew Whitaker. This Class Threads vs. events is an ongoing debate  So, neat-and-tidy answers aren’t necessarily available.
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.
Asynchrony in (ASP).NET Aliaksandr
Code Development for High Performance Servers Topics Multithreaded Servers Event Driven Servers Example - Game Server code (Quake) A parallelization exercise.
Patterns of Parallel Programming with.NET 4 Stephen Toub Principal Architect Parallel Computing Platform Microsoft Corporation
CIS NET Applications1 Chapter 7 – Asynchronous Calls.
APLIKACIJE KOJE SU IZVAN SEBE Domagoj Pavlešić, dizzy.hr.
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.
Introduction to ASP.NET development. Background ASP released in 1996 ASP supported for a minimum 10 years from Windows 8 release ASP.Net 1.0 released.
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.
V 1.0 OE-NIK HP 1 Advanced Programming Fundamentals of parallel execution Processes Threads.
Asynchronous Programming Writing Concurrent Code in C# SoftUni Team Technical Trainers Software University
TechEd /6/2018 6:15 AM © 2013 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered trademarks.
Advanced Topics in Concurrency and Reactive Programming: Asynchronous Programming Majeed Kassis.
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
null, true, and false are also reserved.
Asynchronous programming
Asynchronous Programming
Week 01 Node.js Week 01
03 | Async Programming & Networking Intro
Lecture 20 Parallel Programming CSE /8/2019.
.NET Core Summer event 2019 – Brno, CZ
Presentation transcript:

Cole Durdan

 What is asynchronous programming?  Previous patterns  Task Based Async Programming .NET 4.5 Keywords  What happens in an async. method?  Demo

 “Asynchronous” means API does not block calling thread  Multithreading? ◦ Not Necessarily  Doesn’t lock UI on large computations Synchronous: |----A-----| |-----B | | C------| Asynchronous: |----A-----| |-----B | | C------|

 Web Access  Database Requests  Working with files  Working with images  WCF programming  Working with sockets  With UI that need to be responsive

 The Asynchronous Programming Model (APM) ◦ BeginMethodName and EndMethodName ◦ IAsyncResult ◦ Keep track of states  The Event based Asynchronous Pattern (EAP) ◦ Assigns delegates to event handlers that are invoked when an event is triggered ◦ introduced in the.NET Framework version 2.0

 public static IAsyncResult BeginCopyTo(Stream source, Stream destination) { var tcs = new TaskCompletionSource(); byte[] buffer = new byte[0x1000]; Action readWriteLoop = null; readWriteLoop = iar => { try { for (bool isRead = iar == null; ; isRead = !isRead) { switch (isRead) { case true: iar = source.BeginRead(buffer, 0, buffer.Length, readResult => { if (readResult.CompletedSynchronously) return; readWriteLoop(readResult); }, null); if (!iar.CompletedSynchronously) return; break; case false: int numRead = source.EndRead(iar); if (numRead == 0) { tcs.TrySetResult(true); return; } iar = destination.BeginWrite(buffer, 0, numRead, writeResult => { try { if (writeResult.CompletedSynchronously) return; destination.EndWrite(writeResult); readWriteLoop(null); } catch(Exception e) { tcs.TrySetException(e); } }, null); if (!iar.CompletedSynchronously) return; destination.EndWrite(iar); break; } } } } catch(Exception e) { tcs.TrySetException(e); } }; readWriteLoop(null); return tcs.Task; } public static void EndCopyTo(IAsyncResult asyncResult) { ((Task)asyncResult).Wait(); }

public static async void CopyToAsync(Stream source, Stream destination) { byte[] buffer = new byte[0x1000]; int numRead; while((numRead = await source.ReadAsync(buffer, 0, buffer.Length)) > 0) { await destination.WriteAsync(buffer, 0, numRead); } }

 A lot of extra code  Difficult to… ◦ Read ◦ Write ◦ Maintain ◦ Debug

 Relies on the Task Parallel Library (TPL)  System.Threading  System.Threading.Task namespace ◦ Introduced in.NET 4.0 Framework ◦ New support in 4.5  TPL is the preferred way to write multithreaded and parallel code

 Keywords ◦ “Async” ◦ “Await”  Return Type of Object Task ◦ Task if sub or void ◦ Task  Naming Convention ◦ MethodNameAsync()

Synchronous Method: int GetInt() { int number = GetNumber(); DoIndependentWork(); return number; } Asynchronous Method: async Task GetIntAsync() { Task getNumTask = GetNumberAsync(); DoIndependentWork(); int number = await getNumTask return number; }

 Mark async method with Async or async  Marked method can use Await or await

async Task AccessTheWebAsync() { HttpClient client = new HttpClient(); Task getStringTask = client.GetStringAsync(" DoIndependentWork(); string urlContents = await getStringTask; return urlContents.Length; }

Source:

  5-worth-the-await.aspx 5-worth-the-await.aspx  asynchronous-methods-in-aspnet-45 asynchronous-methods-in-aspnet-45   Programming-in-Csharp-5-0-using-async Programming-in-Csharp-5-0-using-async

 Asynchronous ◦ Multi-Threaded