14

C# Async, un an après

Embed Size (px)

DESCRIPTION

Dans sa dernière mouture, C# vous propose depuis un an le support de l’asynchronisme. Bien qu’ayant moi-même déjà présenté des sessions sur ce sujet, je vous propose ici de vous partager une compréhension plus avancée ainsi que des nouveaux modèles de programmation que j’ai pu découvrir et mettre en œuvre dans mes propres projets.

Citation preview

Page 1: C# Async, un an après
Page 2: C# Async, un an après

Code/Developpement

C# Async, un an après

Mitsuru FURUTACTO

[email protected], http://mitsufu.wordpress.com,

@mitsufu

Page 3: C# Async, un an après

Code/developpement#mstechdays

Depuis votre smartphone sur :http://notes.mstechdays.fr

De nombreux lots à gagner toute les heures !!!Claviers, souris et jeux Microsoft…

Merci de nous aider à améliorer les Techdays !

Donnez votre avis !

Page 4: C# Async, un an après

Code/developpement#mstechdays

• Introduction/Rappels• Comment ça marche ?• Synchronisation

– SynchronizableThread• SequentialTask

– Relance automatique sans thread supplémentaire• Q&A

Agenda

Page 5: C# Async, un an après

Code/developpement#mstechdays

• Intégré à C# 5/.Net 4.5• Evolution du langage + librairies• Asynchrone != parallèle• Async/Await

– Écriture linéaire des callbacks asynchrones (mono ou multi-thread)

– Chainage d’appels à travers un moteur à état– Encapsulation automatique de Task pour les méthodes async

• Awaitable pattern

Introduction/Rappels

Page 6: C# Async, un an après

démoCode/developpement

#mstechdays

Rappels

Page 7: C# Async, un an après

Code/developpement#mstechdays

• “Awaitable” pattern– public object GetAwaiter();

• INotifyCompletion• Public bool IsCompleted { get; }• public object GetResult();

Comment ça marche ?

public interface IAwaitable<out TResult>{ IAwaiter<TResult> GetAwaiter();}public interface IAwaiter<out TResult> : INotifyCompletion // or ICriticalNotifyCompletion{ bool IsCompleted { get; } TResult GetResult();}

Page 8: C# Async, un an après

démoCode/developpement

#mstechdays

manuellement

« Awaitable » pattern

Page 9: C# Async, un an après

Code/developpement#mstechdays

• Par méthode d’extension• CallContext

– Logical context

Comment ça marche ?

Page 10: C# Async, un an après

démoCode/developpement

#mstechdays

Par méthode d’extension

« Awaitable » pattern

Page 11: C# Async, un an après

Code/developpement#mstechdays

• Le retour d’un await revient automatiquement au thread appellant– Par quel mécanisme ?– Notion “ambiante”

• Qu’est-ce le SynchronizationContext ?• ConfigureAwait()

• Contexte de synchronization inexistant ?– Comportement par défaut– Comment en créer un soi-même ?

Contexte de synchronisation

Page 12: C# Async, un an après

démoCode/developpement

#mstechdays

SynchronizableThread

Page 13: C# Async, un an après

démoCode/developpement

#mstechdays

SequentialTask

Page 14: C# Async, un an après

© 2014 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

Digital is business