Async Development con Visual Studio 2012
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

Async Development con Visual Studio 2012

  • 1,257 views
Uploaded 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......

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.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
1,257
On Slideshare
727
From Embeds
530
Number of Embeds
7

Actions

Shares
Downloads
5
Comments
0
Likes
0

Embeds 530

http://www.vifani.com 504
http://vifani.com 11
https://www.linkedin.com 6
http://www.linkedin.com 5
http://feeds.feedburner.com 2
http://translate.googleusercontent.com 1
http://localhost 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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