SlideShare a Scribd company logo
Async CTP
.NET Software engineerEmailkristof@kristofmattei.bekristof.mattei@ordina.beWebsitewww.kristofmattei.beTwitter@Snakiej
Why do asynchronous programming?2 reasons: We’re living in a multicore worldOffload work to other processorsWe’re living in a async worldThings take timeWe don’t want the user’s UI to be blocked when we’re doing somethingAccess a webserviceWrite something to disc
Synchronous vs. AsynchronousSTOPvar data = DownloadData(...);ProcessData(data);ProcessDataDownloadDataThreadDownloadDataAsyncProcessDataDownloadDataAsync(... , data => {ProcessData(data);});Thread
What is the Async CTPSyntactic sugar for .NET 4 at the momentIn C# and VB.NETWill be part of the .NET 5To make easy asynchronous calls without callbacksUses Task, Task<T> or void (fire and forget) to represent ongoing operationsCan be async I/O, backgroundworker, anything you would want to offload.Single object which has status (busy, done, …), exceptions and result
Demo
New thingsasyncTo define a function that awaits a TaskawaitTo wait for a task to complete
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;}
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;}
Old vs new demo
The old wayCreate WebClientUse the async method with callbackIn the callback consume the result
The new async wayShorterEasier to understandNo more callbacksEasier access to exceptions, just wrap it in a try catch block
Unifying AsynchronyAsynchronous MethodsTaskAn asynchronous scenarioScrape YouTube for video linksDownload two or more videos concurrentlyCreate a mashup from downloaded videosSave the resulting videoCPUNetworkI/OComposite
Unifying AsynchronyTaskCPUNetworkI/OCompositetry {string[] videoUrls = awaitScrapeYoutubeAsync(url);   // Network-boundTask<Video> t1 = DownloadVideoAsync(videoUrls[0]);// Start two downloadsTask<Video> t2 = DownloadVideoAsync(videoUrls[1]);Video[] vids = awaitTask.WhenAll(t1, t2);            // Wait for bothVideo v = awaitMashupVideosAsync(vids[0], vids[1]);  // CPU-boundawaitv.SaveAsync(textbox.Text);                      // IO-bound}catch (WebException ex) {ReportError(ex);}
Set-up2 thingsasync methodor 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 finishedYou can only do await inside a async functionAsync functions without await will execute synchronously
Nice to knowDownload linkSpec linkAsync CTP doesn’t play nice with ReSharperCommand window  Resharper_DisableIt’s a CTPFunction names can change(and everything else too!)Modifies compiler - can break other CTPs
Follow Ordina…17Share your thoughts via #SOFTC Follow us on twitter: @OrdinaBEFind the presentationson www.slideshare.net/ordinaBeBe informed at www.linkedin.com/company/ordina-belgium

More Related Content

PPTX
Ordina SOFTC Presentation - SQL CLR
PPTX
Ordina SOFTC Presentation - TFS and JAVA, better together
PDF
Web Applications with Eclipse RT and Docker in the Cloud
PDF
Short journey into the serverless world
PDF
Ansible for networks
PDF
F5 Automation and service discovery
PDF
Ansible 2.2
PDF
OSDC.no 2015 introduction to node.js workshop
Ordina SOFTC Presentation - SQL CLR
Ordina SOFTC Presentation - TFS and JAVA, better together
Web Applications with Eclipse RT and Docker in the Cloud
Short journey into the serverless world
Ansible for networks
F5 Automation and service discovery
Ansible 2.2
OSDC.no 2015 introduction to node.js workshop

What's hot (20)

PPTX
Nodejs intro
PDF
Whatthestack using Tempest for testing your OpenStack deployment
PDF
Introduction to node js - From "hello world" to deploying on azure
PDF
Multiple django applications on a single server with nginx
PDF
Generating Visual Studio Code Extensions for Xtext DSLs
PDF
Ansible container
PPTX
Provisioning, deploying and debugging node.js applications on azure
PPTX
Node.js kubernetes-cloud all the buzzwords coming together with microsoft azure
PDF
GlassFish Embedded API
PDF
Golang @ Tokopedia
PDF
HKG15-204: OpenStack: 3rd party testing and performance benchmarking
PPTX
Parallel Running Automation Solution with Docker, Jenkins and Zalenium
PDF
AdminBits & codequack.com
PPTX
Node js introduction
PDF
CI : the first_step: Auto Testing with CircleCI - (MOSG)
PPTX
DockerCon EU 2017 - Docker on Windows workshop
PDF
Understanding Non Blocking I/O with Python
PDF
Node4J: Running Node.js in a JavaWorld
PDF
Ansible-for-openstack
PDF
VMware compute driver for OpenStack
Nodejs intro
Whatthestack using Tempest for testing your OpenStack deployment
Introduction to node js - From "hello world" to deploying on azure
Multiple django applications on a single server with nginx
Generating Visual Studio Code Extensions for Xtext DSLs
Ansible container
Provisioning, deploying and debugging node.js applications on azure
Node.js kubernetes-cloud all the buzzwords coming together with microsoft azure
GlassFish Embedded API
Golang @ Tokopedia
HKG15-204: OpenStack: 3rd party testing and performance benchmarking
Parallel Running Automation Solution with Docker, Jenkins and Zalenium
AdminBits & codequack.com
Node js introduction
CI : the first_step: Auto Testing with CircleCI - (MOSG)
DockerCon EU 2017 - Docker on Windows workshop
Understanding Non Blocking I/O with Python
Node4J: Running Node.js in a JavaWorld
Ansible-for-openstack
VMware compute driver for OpenStack
Ad

Similar to Ordina SOFTC Presentation - Async CTP (20)

PDF
Advanced iOS Build Mechanics, Sebastien Pouliot
PPTX
Scaling asp.net websites to millions of users
PDF
Release with confidence
PPTX
Async programming and python
ODP
Power ai image-pipeline
PPTX
CTU June 2011 - C# 5.0 - ASYNC & Await
PDF
Play Framework: async I/O with Java and Scala
PPTX
Binary Studio Academy: Concurrency in C# 5.0
PPTX
C# 5 deep drive into asynchronous programming
PPTX
Ddd melbourne 2011 C# async ctp
PDF
Improving the Accumulo User Experience
PDF
Angular performance slides
PPTX
Windows Phone 8 - 3.5 Async Programming
PDF
Angular Optimization Web Performance Meetup
PPTX
End to-end async and await
PDF
Netty - anfix tech&beers
PPTX
Azure serverless Full-Stack kickstart
PDF
Kubernetes laravel and kubernetes
PPT
香港六合彩 &raquo; SlideShare
PPTX
C++ Coroutines
Advanced iOS Build Mechanics, Sebastien Pouliot
Scaling asp.net websites to millions of users
Release with confidence
Async programming and python
Power ai image-pipeline
CTU June 2011 - C# 5.0 - ASYNC & Await
Play Framework: async I/O with Java and Scala
Binary Studio Academy: Concurrency in C# 5.0
C# 5 deep drive into asynchronous programming
Ddd melbourne 2011 C# async ctp
Improving the Accumulo User Experience
Angular performance slides
Windows Phone 8 - 3.5 Async Programming
Angular Optimization Web Performance Meetup
End to-end async and await
Netty - anfix tech&beers
Azure serverless Full-Stack kickstart
Kubernetes laravel and kubernetes
香港六合彩 &raquo; SlideShare
C++ Coroutines
Ad

More from Ordina Belgium (14)

PPTX
Ordina SOFTC Presentation - UsingGeoData_ReportBuilder
PPTX
Ordina SOFTC Presentation - SharePoint - Automated scripted deployment
PPTX
Ordina SOFTC Presentation - Developing against SharePoint 2010 Lists
PPTX
Ordina SOFTC Presentation - SharePoint 2010 Architecture
PPTX
Ordina SOFTC Presentation - Office 365
PPTX
Ordina SOFTC Presentation - Desktop Virtualization
PPTX
Ordina SOFTC Presentation - Demand management using workflow Project Server 2010
PPTX
Ordina SOFTC Presentation - Demand management using workflow Project Server 2010
PPTX
Ordina SOFTC Presentation - Bridging the project and work management gap
PPTX
Ordina SOFTC Presentation - Visual Studio LightSwitch
PPTX
Ordina SOFTC Presentation - Building a WP7 app - Lessons learned
PPTX
Ordina SOFTC Presentation - Windows Phone 7 and the cloud
PPTX
Ordina SOFTC Presentation - Moving a legacy application into the cloud
PPTX
Ordina SOFTC Presentation - Deployment with TFS Build and Workflow
Ordina SOFTC Presentation - UsingGeoData_ReportBuilder
Ordina SOFTC Presentation - SharePoint - Automated scripted deployment
Ordina SOFTC Presentation - Developing against SharePoint 2010 Lists
Ordina SOFTC Presentation - SharePoint 2010 Architecture
Ordina SOFTC Presentation - Office 365
Ordina SOFTC Presentation - Desktop Virtualization
Ordina SOFTC Presentation - Demand management using workflow Project Server 2010
Ordina SOFTC Presentation - Demand management using workflow Project Server 2010
Ordina SOFTC Presentation - Bridging the project and work management gap
Ordina SOFTC Presentation - Visual Studio LightSwitch
Ordina SOFTC Presentation - Building a WP7 app - Lessons learned
Ordina SOFTC Presentation - Windows Phone 7 and the cloud
Ordina SOFTC Presentation - Moving a legacy application into the cloud
Ordina SOFTC Presentation - Deployment with TFS Build and Workflow

Ordina SOFTC Presentation - Async CTP

  • 3. Why do asynchronous programming?2 reasons: We’re living in a multicore worldOffload work to other processorsWe’re living in a async worldThings take timeWe don’t want the user’s UI to be blocked when we’re doing somethingAccess a webserviceWrite something to disc
  • 4. Synchronous vs. AsynchronousSTOPvar data = DownloadData(...);ProcessData(data);ProcessDataDownloadDataThreadDownloadDataAsyncProcessDataDownloadDataAsync(... , data => {ProcessData(data);});Thread
  • 5. What is the Async CTPSyntactic sugar for .NET 4 at the momentIn C# and VB.NETWill be part of the .NET 5To make easy asynchronous calls without callbacksUses Task, Task<T> or void (fire and forget) to represent ongoing operationsCan be async I/O, backgroundworker, anything you would want to offload.Single object which has status (busy, done, …), exceptions and result
  • 7. New thingsasyncTo define a function that awaits a TaskawaitTo 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;}
  • 10. Old vs new demo
  • 11. The old wayCreate WebClientUse the async method with callbackIn the callback consume the result
  • 12. The new async wayShorterEasier to understandNo more callbacksEasier access to exceptions, just wrap it in a try catch block
  • 13. Unifying AsynchronyAsynchronous MethodsTaskAn asynchronous scenarioScrape YouTube for video linksDownload two or more videos concurrentlyCreate a mashup from downloaded videosSave the resulting videoCPUNetworkI/OComposite
  • 14. Unifying AsynchronyTaskCPUNetworkI/OCompositetry {string[] videoUrls = awaitScrapeYoutubeAsync(url); // Network-boundTask<Video> t1 = DownloadVideoAsync(videoUrls[0]);// Start two downloadsTask<Video> t2 = DownloadVideoAsync(videoUrls[1]);Video[] vids = awaitTask.WhenAll(t1, t2); // Wait for bothVideo v = awaitMashupVideosAsync(vids[0], vids[1]); // CPU-boundawaitv.SaveAsync(textbox.Text); // IO-bound}catch (WebException ex) {ReportError(ex);}
  • 15. Set-up2 thingsasync methodor 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 finishedYou can only do await inside a async functionAsync functions without await will execute synchronously
  • 16. Nice to knowDownload linkSpec linkAsync CTP doesn’t play nice with ReSharperCommand window  Resharper_DisableIt’s a CTPFunction names can change(and everything else too!)Modifies compiler - can break other CTPs
  • 17. Follow Ordina…17Share your thoughts via #SOFTC Follow us on twitter: @OrdinaBEFind the presentationson www.slideshare.net/ordinaBeBe informed at www.linkedin.com/company/ordina-belgium

Editor's Notes

  • #4: UI that is blocking while downloading something-&gt; Some browser window waits for an operation -&gt; annoyingMultiple CPUs -&gt; utilise that power
  • #7: Point out keywords0-Go
  • #11: TitleGrabber