async development con visual studio 2012
DESCRIPTION
La nuova versione di Visual Studio introduce il supporto al .NET Framework 4.5, a C# 5 e Visual Basic 11, che con nuove parole chiavi e nuovi compilatori, aprono le porte alla programmazione asincrona Task based. In questa sessione verranno presentate le funzionalità legate a questo pattern, facendo un parallelo con gli approcci che fino ad oggi sono stati impiegati per risolvere i medesimi problemi.TRANSCRIPT
![Page 1: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/1.jpg)
Async DevelopmentRaffaele Fanizziwww.vifani.com
![Page 2: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/2.jpg)
Agenda Introduzione Perché la programmazione asincrona? Operazioni I/O bound vs CPU bound Un po di storia
Async Programming Model (APM) Event-based Async Pattern (EAP)
Task-based Asynchronous Pattern (TAP) Flusso di esecuzione dei metodi async API coinvolte
![Page 3: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/3.jpg)
Cosa significa «concorrenza»?
La capacità di eseguire o far sembrare in esecuzione due operazioni nello stesso momento
Introduzione
![Page 4: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/4.jpg)
Cosa significa «asincrono»?
La capacità di eseguire un’operazione senza che il richiedente resti in attesa della fine della stessa
Introduzione
![Page 5: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/5.jpg)
• Le operazioni asincrone possono condividere un singolo thread per eseguire più operazioni
• L’obiettivo è quello di sfruttare al massimo un singolo thread, senza bloccarlo quando è in attesa di un risultato
• Asincrono != Multithreading
Perché la programmazione asincrona?
![Page 6: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/6.jpg)
• Tanti task possono essere eseguiti insieme, ma non sempre ogni task ha bisogno di un suo thread
• L’uso spregiudicato di nuovi thread non è sempre la risposta migliore
• I thread sono costosi da creare (1 MB di stack per ogni thread)
• I thread sono costosi da gestire (lock, ReadWriterLockSlim, context switching)
Perché la programmazione asincrona?
![Page 7: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/7.jpg)
• Perché siamo obbligati a farlo in alcuni casi
• Per migliorare l’esperienza utente lato client
• Per sfruttare al meglio i worker thread lato server
Perché la programmazione asincrona?
![Page 8: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/8.jpg)
• CPU bound: tempo speso per l’esecuzione di elaborazioni complesse (codec video, grafica 3D, FFT, ecc…) => ideali per il multithreading
• I/O bound: tempo speso ad attendere la risposta da periferiche di I/O come il disco, la rete (web server, database server, ecc…) => ideali per la programmazione asincrona
Operazioni I/O bound vs CPU bound
![Page 9: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/9.jpg)
• Disponibile fin dal .NET Framework 1.0
• Lo ritroviamo nelle chiamate che cominciano per Begin/End
• Restituiscono un IAsyncResult per verificare lo stato dell’esecuzione
• Callback-based
Async Programming Model (APM)
![Page 10: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/10.jpg)
delegate AsyncCallback(IAsyncResult);
interface IAsyncResult {
object AsyncState { get; }
WaitHandle AsyncWaitHandle { get; }
bool CompletedSynchronously { get; }
bool IsCompleted { get; }
}
void BeginXxx(arg1, arg2, ..., AsyncCallback, state);
TResult EndXxx(IAsyncResult);
Async Programming Model (APM)
![Page 11: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/11.jpg)
public static void Transfer(string url, Stream streamOut) {
var request = WebRequest.Create(url);
using (var response = request.GetResponse())
{
var streamIn = response.GetResponseStream();
var size = 1024;
var buffer = new byte[size];
while (true)
{
var lengthRead = streamIn.Read(buffer, 0, size);
if (lengthRead <= 0) break;
streamOut.Write(buffer, 0, lengthRead);
}
}
}
Codice sincrono
![Page 12: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/12.jpg)
IAsyncResult BeginTransfer(url, streamOut, ..., callback) {
// keep context and BeginGetResponse
}
void EndGetResponse(...) {
// get streamIn and BeginRead
}
void EndRead(...) {
// data read and BeginWrite
}
void EndWrite(...) {
// data wrote and BeginRead
}
Codice asincrono APM
![Page 13: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/13.jpg)
public void Transfer(string url, Stream streamOut) { try { var request = WebRequest.Create(url); using (var response = request.GetResponse()) { var streamIn = response.GetResponseStream(); var size = 1024; var buffer = new byte[size]; while (true) { var lengthRead = streamIn.Read(buffer, 0, size); if (lengthRead <= 0) break; streamIn.Write(buffer, 0, lengthRead); } } } catch { // ... } }
Codice sincrono robusto
![Page 14: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/14.jpg)
IAsyncResult BeginTransfer(url, streamOut, ..., callback) {
try { ... } catch { ... }
}
void EndGetResponse(...) {
try { ... } catch { ... }
}
void EndRead(...) {
try { ... } catch { ... }
}
void EndWrite(...) {
try { ... } catch { ... }
}
Codice asincrono robusto APM
![Page 15: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/15.jpg)
• Disponibile fin dal .NET Framework 1.0
• Lo ritroviamo nelle chiamate con suffisso Async e relativo evento Completed
• Difficile capire lo stato dell’esecuzione
• Callback-based
Event-based Async Pattern (EAP)
![Page 16: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/16.jpg)
class XxxCompletedEventArgs : EventArgs { Exception Error { get; } TResult Result { get; }}class Worker { event EventHandler<XxxCompletedArgs> XxxCompleted; void XxxAsync(arg1, arg2, ...);}
Event-based Async Pattern (EAP)
![Page 17: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/17.jpg)
Codice asincrono (EAP)
![Page 18: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/18.jpg)
• Perché i precedenti pattern asincroni non hanno avuto successo?
• Produttività e pigrizia: bisogna scrivere più righe di codice, più metodi ed è più difficile gestire le eccezioni, il progresso e la cancellazione
• Resistenza psicologica: nella nostra mente identifichiamo una chiamata con un’operazione. Eventi e callback non sono naturalmente accettati
Un po’ di storia
![Page 19: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/19.jpg)
Task-based Asynchronous Pattern (TAP) Disponibile fin dal .NET Framework 4, raggiunge piena maturazione nella versione 4.5
Si basa pesantemente sul namespace System.Threading.Task
Si sfrutta al meglio con le parole chiavi supportate dai compilatori C# 5 e VB 11
E’ da considerarsi lo standard di riferimento per tutti i nuovi sviluppi
![Page 20: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/20.jpg)
Task-based Asynchronous Pattern (TAP) La TAP si basa su metodi con le seguenti caratteristiche
Inizia con “async [void|Task|Task<T>]” Il nome del metodo finisce con “Async” I parametri possono opzionalmente avere un CancellationToken e un IProgress<T>
async [void|Task|Task<T>] MethodNameAsync(parameters[, CancellationToken ct, IProgress<T> prog])
![Page 21: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/21.jpg)
Task-based Asynchronous Pattern (TAP) async – Indica che un metodo ha un punto nel quale può essere interrotto
await – E’ usato prima di un’istanza di tipo Task e sospende l’esecuzione del metodo corrente fino a quando un task non si conclude
![Page 22: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/22.jpg)
Flusso di esecuzione dei metodi async I compilatori di C# 5 e VB 11 non fanno altro che trasformare il codice successivo ad un await in una callback del task al quale l’await si riferisce
La prima await causa immediatamente l’uscita dal metodo in esecuzione
Completato il task, l’esecuzione del metodo prosegue normalmente nello stesso SynchronizationContext del chiamante
![Page 23: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/23.jpg)
Flusso di esecuzione dei metodi async
![Page 24: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/24.jpg)
public static void Transfer(string url, Stream streamOut) {
var request = WebRequest.Create(url);
using (var response = request.GetResponse())
{
var streamIn = response.GetResponseStream();
var size = 1024;
var buffer = new byte[size];
while (true)
{
var lengthRead = streamIn.Read(buffer, 0, size);
if (lengthRead <= 0) break;
streamOut.Write(buffer, 0, lengthRead);
}
}
}
Codice sincrono
![Page 25: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/25.jpg)
public static async void Transfer(string url, Stream streamOut) {
var request = WebRequest.Create(url);
using (var response = await request.GetResponseAsync())
{
var streamIn = response.GetResponseStream();
var size = 1024;
var buffer = new byte[size];
while (true)
{
var lengthRead = await streamIn.ReadAsync(buffer, 0, size);
if (lengthRead <= 0) break;
await streamOut.WriteAsync(buffer, 0, lengthRead);
}
}
}
Codice asincrono (TAP)
![Page 26: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/26.jpg)
Task-based Asynchronous Pattern (TAP) Esistono alcune limitazioni/regole
I metodi async devono avere al loro interno almeno un await
Niente parametri ref o out
![Page 27: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/27.jpg)
API Coinvolte .NET Framework 4.5
System.IO.Stream System.IO.TextReader System.Xml.XmlReader System.Net.Mail System.Net.Http.HttpClient System.Net.WebSockets
![Page 28: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/28.jpg)
API Coinvolte ASP.NET MVC 4
Controller con metodi asincroni ADO.NET
DbConnection DbCommand DbDataReader
Windows 8
![Page 29: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/29.jpg)
Domande ?
![Page 30: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/30.jpg)
Riferimenti Raffaele Fanizzi – Il mio blogwww.vifani.com
Asynchronous Programming with Async and Awaithttp://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspx
Implementing the Task-based Asynchronous Patternhttp://msdn.microsoft.com/en-us/library/hh873177.aspx
![Page 31: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/31.jpg)
Grazie per l’attenzione
![Page 32: Async Development con Visual Studio 2012](https://reader035.vdocumenti.com/reader035/viewer/2022062708/558a1c3ed8b42ac7618b475e/html5/thumbnails/32.jpg)
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, 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.