1 / 55

Asynchroniczność w Windows 8

Asynchroniczność w Windows 8. Jakub Binkowski. O mnie. Jakub Binkowski. 2008 - 2011. Lead .NET Developer. Agenda. Dlaczego asynchronicz-ność ?. Historia: APM, EAP. async i await. ASP.NET MVC. Windows 8 Runtime. Internals i architektura. Dlaczego asynchroniczność?.

alia
Download Presentation

Asynchroniczność w Windows 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Asynchroniczność w Windows 8 Jakub Binkowski

  2. O mnie Jakub Binkowski 2008 - 2011 Lead .NET Developer

  3. Agenda Dlaczego asynchronicz-ność? Historia: APM, EAP async i await ASP.NET MVC Windows 8 Runtime Internalsi architektura

  4. Dlaczego asynchroniczność?

  5. Przykładowa metoda… publicvoidDownloadImageAsPng(stringurl, stringfileName) { varwebclient = newWebClient(); varimageBytes = webclient.DownloadData(url); using(varimageStream = newMemoryStream(imageBytes)) { var bitmap = Bitmap.FromStream(imageStream); bitmap.Save(fileName, ImageFormat.Png); } } Pobranie z sieci Zapis na dysk

  6. Ile to trwa? • Gdyby 1 cykl procesora = 1 sekunda: • Rejestr: 1s • Pamięć: 5-10s • Dysk: 3 miesiące • Sieć: 30 lat

  7. Operacje obliczeniowe Wątek

  8. Operacje I/O synchroniczne Wątek Urządzenie Operacja I/O

  9. Problemy • Różne problemy w różnych środowiskach: • Aplikacje desktop (Metro, WPF, …) • Aplikacje serwerowe (ASP.NET, WCF, …)

  10. Aplikacje desktop • Jest jeden wątek UI • Gdy jest zajęty aplikacja przestaje odpowiadać

  11. Aplikacje serwerowe • Oparte o pulę wątków • Gdy wszystkie wątki z puli są zajęte – żądania się kolejkują • Pula wątków rośnie, ale: • Każdy wątek to 1MB zarezerwowanej pamięci

  12. Przykład – aplikacja ASP.NET • Dla uproszczenia: • 3 procesory • 3 wątki w puli • 2 rodzaje operacji: • Tylko HTML – szybka operacja w pamięci • Dostęp do zewnętrznych zasobów (DB, Web Service) - wole

  13. Przykład – aplikacja ASP.NET Żądania aktualnie obsługiwane Kolejka żądań oczekujących HTML Web Service HTML DB HTML DB HTML CPU = 66%

  14. Przykład – aplikacja ASP.NET HTML Web Service DB HTML HTML DB CPU = 33%

  15. Przykład – aplikacja ASP.NET Żądania, które można by obsłużyć… Web Service DB HTML HTML DB CPU = 0%

  16. Problemy operacji synchronicznych • Desktop  „zawieszenie się” • Server  spadek wydajności • Rozwiązanie – operacje asynchroniczne

  17. Operacje I/O asynchroniczne Wątek 1 Urządzenie Operacja I/O Wątek 2

  18. Krótka historia asynchroniczności w .NET

  19. Asynchroniczność w .NET 1.1 • Asynchronous Programming Model Demo

  20. Asynchronous Programming Model • Wg zespołu Windows FormsAPM jest: • zbyt skomplikowany • nie pasuje do trybu design • wymaga pilnowania wątków

  21. Asynchroniczność w .NET 2.0 • Event-baseAsynchronous Programming Demo

  22. Problem APM i EAP… publicvoidDownloadFile(stringurl) { try { varwebClient = newWebClient(); var data = webClient.DownloadData(url); //carry on... } catch (Exception ex) { //handle error... } }

  23. Problem APM i EAP… publicvoidDownloadFile(stringurl) { try { varwebClient = newWebClient(); webClient.DownloadDataCompleted += DownloadFileCompleted; webClient.DownloadDataAsync(newUri(url)); } catch (Exception ex) { //handle error } } privatevoidDownloadFileCompleted(objectsender, DownloadDataCompletedEventArgs e) { if (e.Error == null) { var data = e.Result; //carry on... } else { //handle error } }

  24. A gdyby można było… • Napisać taki kod jak synchroniczny • Niech kompilator zajmie się asynchronicznością • Niech debugger działa jak gdyby kod był synchroniczny

  25. A gdyby tak można było…

  26. async! publicasyncvoidDownloadFile(stringurl) { try { varwebClient = newWebClient(); var data = awaitwebClient.DownloadDataTaskAsync( newUri(url)); //carry on... } catch (Exception ex) { //handle error... } }

  27. Jak działa async? publicasyncTaskDownloadFile(stringurl) { varwc = newWebClient(); var data = awaitwc.DownloadDataTaskAsync(/*...*/); //carry on... }

  28. async Demo

  29. Co zrobi kompilator? publicTaskDownloadFile(stringurl) { varwc = newWebClient(); vardownloadTask = wc.DownloadDataTaskAsync(/*...*/); varcontinuationTask = downloadTask.ContinueWith(t => { if (t.Exception != null) throwt.Exception.InnerException; //carry on... }); returncontinuationTask; } Uwaga! Bardzo duże uproszczenie.

  30. Metody async publicasyncvoidFireAndForget() { /*...*/ } publicasyncTaskFireAndRemember() { /*...*/ } publicasyncTask<int> FireAndReturn() { /*...*/ return 1; }

  31. asyncTask vs asyncvoid asyncTaskXxx() asyncvoidXxx() • „Fire and forget” • Przeznaczenie: • Event handlery • Kompatybilność wstecz • Można poczekać na zakończenie wykonania (i sprawdzić np. exception)

  32. Anonimowy async? Czemu nie! Func<Task<int>> fireAndReturn = async ()=> { /**/ return 1; };

  33. ASP.NET MVCasync

  34. ASP.NET MVC 3 – kontroler publicclassHomeController : Controller{ publicActionResultDownload()    {varclient = newWebClient(); vardata = client.DownloadData(newUri("…"));//...return View();    }} Potencjalnie długotrwała operacja

  35. ASP.NET MVC 3 – kontroler asynchroniczny publicclassHomeController : AsyncController{ publicvoidDownloadAsync()    {AsyncManager.OutstandingOperations.Increment();var client = newWebClient();client.DownloadDataCompleted += (s, a) =>            {AsyncManager.Parameters["data"] = a.Result;AsyncManager.OutstandingOperations.Decrement();            };client.DownloadDataAsync(newUri("…"));    }publicActionResultAvatarDetailsCompleted(byte[] data)    {//...return View();    }}

  36. ASP.NET MVC 3 • Demo Kontroler synchroniczny Kontroler asynchroniczny

  37. ASP.NET MVC 4 – kontroler synchroniczny(przypomnienie) publicclassHomeController : Controller{ publicActionResultDownload()    {varclient = newWebClient(); vardata = client.DownloadData(newUri("…"));//...return View();    }}

  38. ASP.NET MVC 4 – kontroler asynchroniczny publicclassHomeController : AsyncController{ publicasyncTask<ActionResult>Download()    {varclient = newWebClient(); vardata = awaitclient.DownloadDataTaskAsync(/*…*/);//...return View();    }}

  39. ASP.NET MVC 4 • Demo Kontroler asynchroniczny

  40. Windows Runtimeasync

  41. Async w Windows Runtime User Interface HTML5 / CSS XAML DirectX Controls Data Binding SVG Tiles Input Accessibility Printing Communications & Data Devices Geolocation NFC Printer Contracts XML Web Sensors Portable Devices Streams Networking Notifications Media AtomPub Local & Cloud Storage Capture Visual Effects SMS Background Transfer Transcoding PlayTo Fundamentals App Lifetime Authentication Cryptography Globalization .NET Win32

  42. Asynchroniczność w Windows Runtime Demo

  43. Async w Windows Runtime

  44. IAsync* a Task IAsyncAction Task IAsyncOperation<TResult> Task<TResult>

  45. Internals, architektura

  46. Na jakim wątku wykona się dalej kod? Demo

  47. Pytanie Czy skoro można wywołać na: • Task • IAsyncAction/Operation to czy można wywołać na czymś jeszcze innym?

  48. await – na czym można „czekać”? var my = newMyClass(); await my; publicclassMyClass { publicMyAwaiterGetAwaiter() { /*..*/} } publicclassMyAwaiter : INotifyCompletion { publicboolIsCompleted { get{/*...*/} } publicvoidGetResult() {/*..*/} publicvoidOnCompleted(Actioncontinuation) {/*..*/} } Może też być extensionmethod

  49. Co będzie w zmiennej type? vartask = Task.Run<int>( newFunc<int>(() => { thrownewException(); })); try { awaittask; } catch(Exception ex) { stringtype = ex.GetType().Name; } vartask = Task.Run<int>( newFunc<int>(() => { thrownewException(); })); task.ContinueWith(t => { try { var res = t.Result; } catch (Exception ex) { stringtype = ex.GetType().Name; } });

  50. await a Exception Demo

More Related