Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ordina SOFTC Presentation - Async CTP


Published on

Ordina's Stand out from the Crowd event of May 26th - the presentation, see them all on Slideshare Ordina Belgium

  • Be the first to comment

Ordina SOFTC Presentation - Async CTP

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