AsyncDevelopmentRaffaele Fanizziwww.vifani.com
Cosa significa «concorrenza»?La capacità di eseguire o far sembrare inesecuzione due operazioni nello stesso momento
Cosa significa «asincrono»?La capacità di eseguire un’operazione senza che ilrichiedente resti in attesa della fine della ...
delegate AsyncCallback(IAsyncResult);   interface IAsyncResult {       object AsyncState { get; }       WaitHandle AsyncWa...
public static void Transfer(string url, Stream streamOut)      {           var request = WebRequest.Create(url);          ...
IAsyncResult BeginTransfer(url, streamOut, ..., callback) {       // keep context and BeginGetResponse       }       void ...
public void Transfer(string url, Stream streamOut) {            try {                var request = WebRequest.Create(url);...
IAsyncResult BeginTransfer(url, streamOut, ..., callback) {       try { ... } catch { ... }}void EndGetResponse(...) {    ...
class XxxCompletedEventArgs : EventArgs {    Exception Error { get; }    TResult Result { get; }}class Worker {    event E...
public static void Transfer(string url, Stream streamOut)      {           var request = WebRequest.Create(url);          ...
public static async void Transfer(string url, Stream streamOut)         {           var request = WebRequest.Create(url); ...
www.vifani.comhttp://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspxhttp://msdn.microsoft.com/en-us/library/hh87317...
© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, and other product names are or may be registered tr...
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Async Development con Visual Studio 2012
Upcoming SlideShare
Loading in …5
×

Async Development con Visual Studio 2012

1,346 views

Published on

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.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
1,346
On SlideShare
0
From Embeds
0
Number of Embeds
631
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Async Development con Visual Studio 2012

  1. 1. AsyncDevelopmentRaffaele Fanizziwww.vifani.com
  2. 2. Cosa significa «concorrenza»?La capacità di eseguire o far sembrare inesecuzione due operazioni nello stesso momento
  3. 3. Cosa significa «asincrono»?La capacità di eseguire un’operazione senza che ilrichiedente resti in attesa della fine della stessa
  4. 4. 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);
  5. 5. 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); } } }
  6. 6. 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 }
  7. 7. 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 { // ... } }
  8. 8. IAsyncResult BeginTransfer(url, streamOut, ..., callback) { try { ... } catch { ... }}void EndGetResponse(...) { try { ... } catch { ... }}void EndRead(...) { try { ... } catch { ... }}void EndWrite(...) { try { ... } catch { ... }}
  9. 9. class XxxCompletedEventArgs : EventArgs { Exception Error { get; } TResult Result { get; }}class Worker { event EventHandler<XxxCompletedArgs>XxxCompleted; void XxxAsync(arg1, arg2, ...);}
  10. 10. 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); } } }
  11. 11. 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); } } }
  12. 12. www.vifani.comhttp://msdn.microsoft.com/en-us/library/vstudio/hh191443.aspxhttp://msdn.microsoft.com/en-us/library/hh873177.aspx
  13. 13. © 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 thepart 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.

×