POINT OF FAILURES TOPICS .NET. msdn

Similar documents
Asynchronous Programming Demystified

The Various Faces of the.net Task Parallel Library

An Async Primer. By Bill Wagner August Introduction

UriQuery query.add + query.tostring()

Asynchronous Functions in C#

Asynchronous Programming

IN DEPTH THIRD EDITION. Jon Skeet FOREWORD BY ERIC LIPPERT MANNING

Concurrency Analysis of Asynchronous APIs

C# Asynchronous Programming Model

The Task-based Asynchronous Pattern

Introduction to Coroutines. Roman Elizarov elizarov at JetBrains

Three Ways Roslyn Will Change Your Life

Asynchronous Programming with Async and Await 1 Await Operator 12 Async 15 Accessing the Web by Using Async and Await 18 Extend the Async Walkthrough

Task-based Asynchronous Pattern 1 Implementing the Task-based Asynchronous Pattern 8 Interop with Other Asynchronous Patterns and Types 14

Programming in.net. Microsoft Development Center Serbia programming course. Lesson 8 Parallelism and Threading in.net

COMIC BOOK GRAPHIC NOVEL APPROACH TO ASYNC

Simplifying Asynchronous Programming with Microsoft Visual Studio Async CTP

1 C# 6.0: Practical Guide 6.0. Practical Guide. By: Mukesh Kumar.

Course Hours

Shared Objects & Mutual Exclusion

MCSA Universal Windows Platform. A Success Guide to Prepare- Programming in C# edusum.com

Bringing Together One ASP.NET

Software Exception Flow Analysis for WPF C# Asynchronous Programs Using Microsoft Visual Studio

Problems with Concurrency. February 19, 2014

Kotlin for Android Developers

Fresh Async With Kotlin. Presented at QCon SF, 2017 /Roman JetBrains

Breaking your code in new and exciting ways. Michael Newton

JS Event Loop, Promises, Async Await etc. Slava Kim

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

CSCD 330 Network Programming

SERG. A Study and Toolkit for Asynchronous Programming in C#

ArcGIS Pro SDK for.net Beginning Pro Customization. Charles Macleod

INF 212 ANALYSIS OF PROG. LANGS FUNCTION COMPOSITION. Instructors: Crista Lopes Copyright Instructors.

C # 7, 8, and beyond: language features from design to release to IDE support. Kevin

CSE 142/143 Unofficial Style Guide

Concurrent Programming

CSCD 330 Network Programming

Manage program flow. Have you read page xxi? Objectives in this chapter:

ASYNCHRONOUS PROGRAMMING IN C# 5 WITHOUT USE OF MULTIPLE THREADS

The Future of Parallel Programming in the.net Framework. Igor Ostrovsky Software Engineer Microsoft Corporation

Building a mobile enterprise application with Xamarin.Forms, Docker, MVVM and.net Core. Gill

Laboratory 5: Implementing Loops and Loop Control Strategies

COMP 322 / ELEC 323: Fundamentals of Parallel Programming

VS08 This One Goes to Going Parallel with PFX, PLINQ, TPL and Async Keywords

Concurrent Programming in C++ Venkat

C# Programming in the.net Framework

C# Threading. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

Mobile SDK for Xamarin - SSC 1.1 Rev: September 13, Mobile SDK for Xamarin - SSC 1.1

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

How to download a file in Xamarin

DON'T BLOCK YOUR MOBILES AND INTERNET OF THINGS

Asynchronous OSGi: Promises for the masses. Tim Ward.

Chapter 1 Getting Started

CS61B, Spring 2003 Discussion #17 Amir Kamil UC Berkeley 5/12/03

Object Oriented Methods with UML. Lecture -4

! How is a thread different from a process? ! Why are threads useful? ! How can POSIX threads be useful?

Converting Parallel Code from Low-Level Abstractions to Higher-Level Abstractions

DOT NET Syllabus (6 Months)

Introduce C# as Object Oriented programming language. Explain, tokens,

Make the Most of OpenMP Tasking. Sergi Mateo Bellido Compiler engineer

COMP30112: Concurrency Topics 4.1: Concurrency Patterns - Monitors

Dynamic Storage Exercise

20-CS Programming Languages Fall 2016

INTRODUCTION TO.NET. Domain of.net D.N.A. Architecture One Tier Two Tier Three Tier N-Tier THE COMMON LANGUAGE RUNTIME (C.L.R.)

ArcGIS Pro SDK for.net: Add-in Fundamentals and Development Patterns. Wolf Kaiser, Uma Harano

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

Effective Use of. Scott Weber

Want To Be a Better Programmer? Lars Bak and Kasper Lund, Inventors of Dart, Software engineers at Google

Threads & Networking

Java Threads. COMP 585 Noteset #2 1

Computation at the Speed of Monads July Computation at the Speed of Monads. Brigham Young University - Idaho.

Module 1. An Introduction to C# Module 2. Classes and Objects. Vasundhara Sector 14-A, Plot No , Near Vaishali Metro Station,Ghaziabad

Parallelizing Ocean plug-in computations using the Background Worker + PFX pattern

Entity Configuration Configure the account entity in D365 with fields to hold the cloud agreement status. Two Value OptionSet

GStreamer Element States How do they work in detail?

ArcGIS Pro SDK for.net: Asynchronous Programming and MVVM Patterns in Pro. Wolfgang Kaiser

Lecture Topics. Administrivia

Concurrency: An Overview

CSE 413 Winter 2001 Midterm Exam

TDDB84: Lecture 5. Singleton, Builder, Proxy, Mediator. fredag 27 september 13

ArcGIS Pro SDK for.net Intro and Pro Add-in Programming Patterns. Wolfgang Kaiser

C# Java. C# Types Naming Conventions. Distribution and Integration Technologies. C# C++.NET A C# program is a collection of: C C++ C# Language

Distribution and Integration Technologies. C# Language

Concurrency in Software Designs: How to Avoid Nasty Surprises?

ComponentOne. PdfViewer for WPF and Silverlight

CS 556 Distributed Systems

C#: framework overview and in-the-small features

COMP346 Winter Tutorial 4 Synchronization Semaphores

Swift 5, ABI Stability and

Visual Studio 2010 Silverlight No Symbols Have Been Loaded For This Document

!! How is a thread different from a process? !! Why are threads useful? !! How can POSIX threads be useful?

Microsoft Cloud Workshops

JAVA and J2EE UNIT - 4 Multithreaded Programming And Event Handling

Production 100mph

Multithreaded Programming

Using Expressions Effectively

Kotlin for Android Developers

Systems Programming & Scripting

Concurrent Computing CSCI 201 Principles of Software Development

DAD Lab. 2 Additional C# Topics

Transcription:

1

TOPICS POINT OF FAILURES msdn.net 2

THREADS TASKS msdn.net 3

KEY FEATURES msdn.net 4

TASK CREATION var task = new Task(Func<TResult> func); task.start(); //... task.wait(); var task = Task.Run(Func<TResult> func); //... task.wait(); var task = Task.Factory.StartNew(Func<TResult> func); //... task.wait(); 5

TASK COMPLETION SOURCE Psychic Debugging of Async Methods public Task<int> SomeLibraryMethodAsync() var tcs = new TaskCompletionSource<int>(); Task.Factory.StartNew(() => try int result = SomeLibraryMethod(); tcs.setresult(result); catch (Exception e) // Bug! tcs.setexception(e); ); return tcs.task; 6

ASYNC/AWAIT 7

ASYNC AWAIT private static async Task GetHttpResponseAsync() using (var httpclient = new HttpClient()) var responsetask = httpclient.getasync("https://msdn.microsoft.com"); // Do independent work... var response = await responsetask; Console.WriteLine(response.Headers); 8

PROS private static async Task GetHttpResponseAsync() using (var httpclient = new HttpClient()) var responsetask = httpclient.getasync("https://msdn.microsoft.com"); // Do independent work... var response = await responsetask; Console.WriteLine(response.Headers); 9

CONS 1 2 private static async Task GetHttpResponseAsync() using (var httpclient = new HttpClient()) 3 var responsetask = httpclient.getasync("https://msdn.microsoft.com"); // Do independent work... 4 7 5 var response = await responsetask; Console.WriteLine(response.Headers); 6 Async and Await (msdn) public async HttpResponseMessage GetAsync(string url) 10

private static async Task GetHttpResponseAsync() using (var httpclient = new HttpClient()) var responsetask = httpclient.getasync("https://msdn.microsoft.com"); // Do independent work... var response = await responsetask; Console.WriteLine(response.Headers); 11

[AsyncStateMachine(typeof(<GetHttpResponseAsync>d 1)), DebuggerStepThrough] private static Task GetHttpResponseAsync() <GetHttpResponseAsync>d 1 statemachine = new <GetHttpResponseAsync>d 1 <>t builder = AsyncTaskMethodBuilder.Create(), <>1 state = -1 ; statemachine.<>t builder.start<<gethttpresponseasync>d 1>(ref statemachine); return statemachine.<>t builder.task; [CompilerGenerated] private sealed class <GetHttpResponseAsync>d 1 : IAsyncStateMachine Behind the.net 4.5 Async Scene 12

ASYNC VOID private async void ThrowExceptionAsync() throw new InvalidOperationException(); public void CallThrowExceptionAsync() try ThrowExceptionAsync(); catch (Exception) Console.WriteLine("Failed"); 13

For goodness sake stop using async void 14

ASYNC LAMBDA var secs = Time(() => Thread.Sleep(1000); ); Console.WriteLine($"Seconds: secs:f6"); Seconds: 1.000361 Seconds: 0.001521 Seconds: 1.006651 var secs2 = Time(async () => await Task.Delay(1000); ); Console.WriteLine($"Seconds: secs2:f6"); public static double Time(Action action) public static double Time(Func<Task> func) 15

ASYNC LAMBDA var t = Task.Factory.StartNew(() => Thread.Sleep(1000); return 42; ); var u = Task.Factory.StartNew(async () => await Task.Delay(1000); return 42; ).Unwrap(); Passing async lambdas 16

private async Task WorkThenWait() await Task.Yield(); Thread.Sleep(1000); Console.WriteLine("work"); await Task.Delay(1000); work started started work completed Asynchronous gotchas in C# public void Demo() var child = WorkThenWait(); Console.WriteLine("started"); child.wait(); Console.WriteLine("completed"); 17

Don't Block on Async Code public class MyController : ApiController private static async Task<JObject> GetJsonAsync(Uri uri) using (var client = new HttpClient()) var jsonstring = await client.getstringasync(uri); return JObject.Parse(jsonString); public async Task<string> GetAsync() var json = await GetJsonAsync(new Uri("http://bing.com")); return json.tostring(); 18

class Accumulator private int m_sum = 0; public int Sum => m_sum; 4 6 Don't mix await and compound assignment public async Task Add(Task<int> value) var temp = await value; m_sum += temp; await value; var task1 = acc.add(tcs1.task); var task2 = acc.add(tcs2.task); tcs1.setresult(2); tcs2.setresult(4); await task1; await task2; Console.WriteLine(acc.Sum); 19

Don t forget to complete your Tasks! Watch out for Signatures! Be aware of your Synchronization context! Think double, less trouble... 20

21