fluid user interfaces with async / await · 2016-06-28 · .net 4.5: task-based asynchronous...

21
Thomas Claudius Huber | Fluid User Interfaces with async / await

Upload: others

Post on 21-Jun-2020

19 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Thomas Claudius Huber |

Fluid User Interfaces

with async / await

Page 2: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Thomas Claudius Huber

Principal Consultant @

Developer, Trainer

Microsoft MVP für

Windows Platform Development

Schwerpunkte: WPF, XAML, WinApps, .NET

www.thomasclaudiushuber.com

@thomasclaudiush

Page 3: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Agenda

Basics

Async / Await im ViewModel

Summary

Page 4: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

PART 1 / 3:

Basics

Page 5: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Asynchron in .NET 1.0

«Asynchronous Programming Model» (APM)

BeginInvoke und EndInvoke

AsyncCallback und IAsyncResult

Bekannt von Streams und Sockets

Page 6: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Asynchron in .NET 2.0

«Event-based asynchronous pattern» (EAP)

Zu einer [...]Async-Methode gibt es ein

[...]Completed-Event

bspw. LoadAsync und LoadCompleted

Page 7: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Asynchron in .NET 2.0

private static void EventBasedAsynchronousPattern()

{

var wc = new WebClient();

wc.DownloadStringCompleted += (sender, eventArgs) =>

{

string html = eventArgs.Result;

// ...

};

wc.DownloadStringAsync(

new Uri("http://www.thomasclaudiushuber.com"));

}

Page 8: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

.NET 4.0: Einführung der

Task Parallel Library (TPL)

Neuer Namespace System.Threading.Tasks

Task startet neuen Thread:

Task.Factory.StartNew

Abstraktion über klassische Threading-Klassen

Demo

Page 9: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

PART 2 / 3:

Async / Await im ViewModel

Page 10: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

.NET 4.5: Task-based Asynchronous

Pattern (TAP)

Baut auf Task Parallel Library (TPL) auf

Sprachunterstützung in C# und VB.NET

async und await

Verfügbar für verschiedene Microsoft-Platformen

Web, Desktop, WinRT, ...

Page 11: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Asynchrone Methoden

Nutzen das async Schlüsselwort

enden konventionsgemäss auf “Async”

Geben die Kontrolle an den Aufrufer zurück

Haben üblicherweise mindestens

eine “await”-Anweisung

Demo

Page 12: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Rückgabetypen asynchroner

Methoden

void – der Aufrufer benötigt keine Info

wird nur für EventHandler verwendet

Task – der Aufrufer erhält Info zum Task-Zustand

bspw. ob der Task «completed» ist

Task<T> - neben dem Task-Zustand erhält der

Aufrufer den Rückgabewert vom Typ T

Demo

Page 13: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Asynchron vs. Multithreading

CPU-lastiges in neuen Thread auslagern

Task.Run / Task.Factory.StartNew

Für andere APIs wird nicht zwingend ein neuer

Thread erstellt, sie sind jedoch asynchron:

bspw. WebClient-Klasse und deren

DownloadStringTaskAsync-Methode

Page 14: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Was passiert im Hintergrund

Await / Async sind für den Compiler

async markiert eine Methode, die der Compiler

ändert

In der Methode wird eine Art Statemachine generiert

basiert auf einer TaskAwaiter-Instanz

Page 15: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Async und User Interfaces

Synchrone Eventhandler blockieren das UI

Touch gegenüber Maus/Tastatur hochempfindlich

Verzeiht keine Verzögerungen

WinRT => alles was länger als 50ms dauern

könnte, ist nur asynchron verfügbar

Page 16: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Ein Async ViewModel erstellen

Hat problemlos geklappt

Ohne weitere Änderungen einfach async / await

genutzt

What about testing?

Demo

Page 17: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Weitere Features der Tasks

Progress-Support

Abbruch mit CancellationToken

Page 18: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

.NET vs WinRT

.NET

Task

Task<T>

WinRT

IAsyncAction

IAsyncOperation

IAsyncActionWithProgress

IAsyncOperationWithProgress

Page 19: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

PART 3 / 3: Summary

Page 20: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Summary

Async / Await ist problemlos im ViewModel einsetzbar

DelegateCommand für Testing angepasst

Programmierung sieht weiterhin synchron aus

Unter der Haube die übliche Komplexität

Page 21: Fluid User Interfaces with async / await · 2016-06-28 · .NET 4.5: Task-based Asynchronous Pattern (TAP) Baut auf Task Parallel Library (TPL) auf Sprachunterstützung in C# und

Fragen & Antworten

Contact:

Thomas Claudius Huber

@thomasclaudiush

[email protected]

www.trivadis.com

www.thomasclaudiushuber.com