The document discusses async programming in .NET using the Async CTP. It explains that async programming is important for multicore processing and handling asynchronous operations like web requests without blocking the UI. The Async CTP allows writing async code using the async and await keywords in C# and VB.NET to simplify asynchronous calls and avoid callbacks. It provides an example of asynchronously downloading and parsing RSS feed XML and compares the old callback-based approach to the new async approach.
3. Why do asynchronous programming? 2 reasons: We’re living in a multicore world Offload work to other processors We’re living in a async world Things take time We don’t want the user’s UI to be blocked when we’re doing something Access a webservice Write something to disc
4. Synchronous vs. Asynchronous STOP var data = DownloadData(...); ProcessData(data); ProcessData DownloadData Thread DownloadDataAsync ProcessData DownloadDataAsync(... , data => { ProcessData(data); }); Thread
5. What is the Async CTP Syntactic sugar for .NET 4 at the moment In C# and VB.NET Will be part of the .NET 5 To make easy asynchronous calls without callbacks Uses Task, Task<T> or void (fire and forget) to represent ongoing operations Can be async I/O, backgroundworker, anything you would want to offload. Single object which has status (busy, done, …), exceptions and result
7. New things async To define a function that awaits a Task await To wait for a task to complete
8. How does it work? async Task<XElement> GetRssAsync(stringurl) { var client = newWebClient(); var task = client.DownloadStringTaskAsync(url); var text = await task; var xml = XElement.Parse(text); return xml; }
9. async Task<XElement> GetRssAsync(string url) { var client = new WebClient(); var task = client.DownloadStringTaskAsync(url); var text = await task; var xml = XElement.Parse(text); return xml; } How does it work? Task<XElement> GetRssAsync(stringurl) { var $builder = AsyncMethodBuilder<XElement>.Create(); var $state = 0; TaskAwaiter<string> $a1; Action $resume = delegate { try { if ($state == 1) goto L1; var client = newWebClient(); var task = client.DownloadStringTaskAsync(url); $state = 1; $a1 = task.GetAwaiter(); if ($a1.BeginAwait($resume)) return; L1: var text = $a1.EndAwait(); var xml = XElement.Parse(text); $builder.SetResult(xml); } catch (Exception $ex) { $builder.SetException($ex); } }; $resume(); return $builder.Task; }
11. The old way Create WebClient Use the async method with callback In the callback consume the result
12. The new async way Shorter Easier to understand No more callbacks Easier access to exceptions, just wrap it in a try catch block
13. Unifying Asynchrony Asynchronous Methods Task An asynchronous scenario Scrape YouTube for video links Download two or more videos concurrently Create a mashup from downloaded videos Save the resulting video CPU Network I/O Composite
14. Unifying Asynchrony Task CPU Network I/O Composite try { string[] videoUrls = awaitScrapeYoutubeAsync(url); // Network-bound Task<Video> t1 = DownloadVideoAsync(videoUrls[0]);// Start two downloads Task<Video> t2 = DownloadVideoAsync(videoUrls[1]); Video[] vids = awaitTask.WhenAll(t1, t2); // Wait for both Video v = awaitMashupVideosAsync(vids[0], vids[1]); // CPU-bound awaitv.SaveAsync(textbox.Text); // IO-bound } catch (WebException ex) { ReportError(ex); }
15. Set-up 2 things async method or it awaits another async function or it awaits a Task(<T>). The await will essentially execute the task and the code BELOW the await will be the callback of when the task is finished You can only do await inside a async function Async functions without await will execute synchronously
16. Nice to know Download link Spec link Async CTP doesn’t play nice with ReSharper Command window Resharper_Disable It’s a CTP Function names can change (and everything else too!) Modifies compiler - can break other CTPs
17. Follow Ordina… 17 Share your thoughts via #SOFTC Follow us on twitter: @OrdinaBE Find the presentationson www.slideshare.net/ordinaBe Be informed at www.linkedin.com/company/ordina-belgium
Hinweis der Redaktion
UI that is blocking while downloading something-> Some browser window waits for an operation -> annoyingMultiple CPUs -> utilise that power