SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Async CTP
.NET Software engineer Email kristof@kristofmattei.be kristof.mattei@ordina.be Website www.kristofmattei.be Twitter @Snakiej
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
Synchronous vs. Asynchronous STOP var data = DownloadData(...); ProcessData(data); ProcessData DownloadData Thread DownloadDataAsync ProcessData DownloadDataAsync(... , data => { ProcessData(data); }); Thread
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
Demo
New things async To define a function that awaits a Task await To 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 way Create WebClient Use the async method with callback In the callback consume the result
The new async way Shorter Easier to understand No more callbacks Easier access to exceptions, just wrap it in a try catch block
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
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); }
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
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
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

Weitere ähnliche Inhalte

Was ist angesagt?

Whatthestack using Tempest for testing your OpenStack deployment
Whatthestack using Tempest for testing your OpenStack deploymentWhatthestack using Tempest for testing your OpenStack deployment
Whatthestack using Tempest for testing your OpenStack deploymentChristian Schwede
 
Introduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azureIntroduction to node js - From "hello world" to deploying on azure
Introduction to node js - From "hello world" to deploying on azureColin Mackay
 
Multiple django applications on a single server with nginx
Multiple django applications on a single server with nginxMultiple django applications on a single server with nginx
Multiple django applications on a single server with nginxroskakori
 
Generating Visual Studio Code Extensions for Xtext DSLs
Generating Visual Studio Code Extensions for Xtext DSLsGenerating Visual Studio Code Extensions for Xtext DSLs
Generating Visual Studio Code Extensions for Xtext DSLsKarsten Thoms
 
Provisioning, deploying and debugging node.js applications on azure
Provisioning, deploying and debugging node.js applications on azureProvisioning, deploying and debugging node.js applications on azure
Provisioning, deploying and debugging node.js applications on azurePatriek van Dorp
 
Node.js kubernetes-cloud all the buzzwords coming together with microsoft azure
Node.js kubernetes-cloud all the buzzwords coming together with microsoft azureNode.js kubernetes-cloud all the buzzwords coming together with microsoft azure
Node.js kubernetes-cloud all the buzzwords coming together with microsoft azurePatriek van Dorp
 
Golang @ Tokopedia
Golang @ TokopediaGolang @ Tokopedia
Golang @ TokopediaQasim Zaidi
 
HKG15-204: OpenStack: 3rd party testing and performance benchmarking
HKG15-204: OpenStack: 3rd party testing and performance benchmarkingHKG15-204: OpenStack: 3rd party testing and performance benchmarking
HKG15-204: OpenStack: 3rd party testing and performance benchmarkingLinaro
 
Parallel Running Automation Solution with Docker, Jenkins and Zalenium
Parallel Running Automation Solution with Docker, Jenkins and ZaleniumParallel Running Automation Solution with Docker, Jenkins and Zalenium
Parallel Running Automation Solution with Docker, Jenkins and ZaleniumEvozon Test Lab
 
CI : the first_step: Auto Testing with CircleCI - (MOSG)
CI : the first_step: Auto Testing with CircleCI - (MOSG)CI : the first_step: Auto Testing with CircleCI - (MOSG)
CI : the first_step: Auto Testing with CircleCI - (MOSG)Soshi Nemoto
 
DockerCon EU 2017 - Docker on Windows workshop
DockerCon EU 2017 - Docker on Windows workshopDockerCon EU 2017 - Docker on Windows workshop
DockerCon EU 2017 - Docker on Windows workshopElton Stoneman
 
Understanding Non Blocking I/O with Python
Understanding Non Blocking I/O with PythonUnderstanding Non Blocking I/O with Python
Understanding Non Blocking I/O with PythonVaidik Kapoor
 
Node4J: Running Node.js in a JavaWorld
Node4J: Running Node.js in a JavaWorldNode4J: Running Node.js in a JavaWorld
Node4J: Running Node.js in a JavaWorldIan Bull
 
Ansible-for-openstack
Ansible-for-openstackAnsible-for-openstack
Ansible-for-openstackUdayendu Kar
 
VMware compute driver for OpenStack
VMware compute driver for OpenStackVMware compute driver for OpenStack
VMware compute driver for OpenStackopenstackindia
 

Was ist angesagt? (20)

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

Ähnlich wie Async CTP Overview - Unifying Asynchrony with Tasks

Advanced iOS Build Mechanics, Sebastien Pouliot
Advanced iOS Build Mechanics, Sebastien PouliotAdvanced iOS Build Mechanics, Sebastien Pouliot
Advanced iOS Build Mechanics, Sebastien PouliotXamarin
 
Scaling asp.net websites to millions of users
Scaling asp.net websites to millions of usersScaling asp.net websites to millions of users
Scaling asp.net websites to millions of usersoazabir
 
Release with confidence
Release with confidenceRelease with confidence
Release with confidenceJohn Congdon
 
Async programming and python
Async programming and pythonAsync programming and python
Async programming and pythonChetan Giridhar
 
CTU June 2011 - C# 5.0 - ASYNC & Await
CTU June 2011 - C# 5.0 - ASYNC & AwaitCTU June 2011 - C# 5.0 - ASYNC & Await
CTU June 2011 - C# 5.0 - ASYNC & AwaitSpiffy
 
Play Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaPlay Framework: async I/O with Java and Scala
Play Framework: async I/O with Java and ScalaYevgeniy Brikman
 
Binary Studio Academy: Concurrency in C# 5.0
Binary Studio Academy: Concurrency in C# 5.0Binary Studio Academy: Concurrency in C# 5.0
Binary Studio Academy: Concurrency in C# 5.0Binary Studio
 
C# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingC# 5 deep drive into asynchronous programming
C# 5 deep drive into asynchronous programmingPraveen Prajapati
 
Ddd melbourne 2011 C# async ctp
Ddd melbourne 2011  C# async ctpDdd melbourne 2011  C# async ctp
Ddd melbourne 2011 C# async ctpPratik Khasnabis
 
Improving the Accumulo User Experience
 Improving the Accumulo User Experience Improving the Accumulo User Experience
Improving the Accumulo User ExperienceAccumulo Summit
 
Angular performance slides
Angular performance slidesAngular performance slides
Angular performance slidesDavid Barreto
 
Windows Phone 8 - 3.5 Async Programming
Windows Phone 8 - 3.5 Async ProgrammingWindows Phone 8 - 3.5 Async Programming
Windows Phone 8 - 3.5 Async ProgrammingOliver Scheer
 
Angular Optimization Web Performance Meetup
Angular Optimization Web Performance MeetupAngular Optimization Web Performance Meetup
Angular Optimization Web Performance MeetupDavid Barreto
 
End to-end async and await
End to-end async and awaitEnd to-end async and await
End to-end async and awaitvfabro
 
Netty - anfix tech&beers
Netty - anfix tech&beersNetty - anfix tech&beers
Netty - anfix tech&beersjorgecarabias
 
Azure serverless Full-Stack kickstart
Azure serverless Full-Stack kickstartAzure serverless Full-Stack kickstart
Azure serverless Full-Stack kickstartDavide Mauri
 
Kubernetes laravel and kubernetes
Kubernetes   laravel and kubernetesKubernetes   laravel and kubernetes
Kubernetes laravel and kubernetesWilliam Stewart
 
香港六合彩 &raquo; SlideShare
香港六合彩 &raquo; SlideShare香港六合彩 &raquo; SlideShare
香港六合彩 &raquo; SlideShareyayao
 

Ähnlich wie Async CTP Overview - Unifying Asynchrony with Tasks (20)

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

Mehr von Ordina Belgium

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

Mehr von Ordina Belgium (14)

Ordina SOFTC Presentation - UsingGeoData_ReportBuilder
Ordina SOFTC Presentation - UsingGeoData_ReportBuilderOrdina SOFTC Presentation - UsingGeoData_ReportBuilder
Ordina SOFTC Presentation - UsingGeoData_ReportBuilder
 
Ordina SOFTC Presentation - SharePoint - Automated scripted deployment
Ordina SOFTC Presentation - SharePoint - Automated scripted deploymentOrdina SOFTC Presentation - SharePoint - Automated scripted deployment
Ordina SOFTC Presentation - SharePoint - Automated scripted deployment
 
Ordina SOFTC Presentation - Developing against SharePoint 2010 Lists
Ordina SOFTC Presentation - Developing against SharePoint 2010 ListsOrdina SOFTC Presentation - Developing against SharePoint 2010 Lists
Ordina SOFTC Presentation - Developing against SharePoint 2010 Lists
 
Ordina SOFTC Presentation - SharePoint 2010 Architecture
Ordina SOFTC Presentation - SharePoint 2010 ArchitectureOrdina SOFTC Presentation - SharePoint 2010 Architecture
Ordina SOFTC Presentation - SharePoint 2010 Architecture
 
Ordina SOFTC Presentation - Office 365
Ordina SOFTC Presentation - Office 365Ordina SOFTC Presentation - Office 365
Ordina SOFTC Presentation - Office 365
 
Ordina SOFTC Presentation - Desktop Virtualization
Ordina SOFTC Presentation - Desktop VirtualizationOrdina SOFTC Presentation - Desktop Virtualization
Ordina SOFTC Presentation - Desktop Virtualization
 
Ordina SOFTC Presentation - Demand management using workflow Project Server 2010
Ordina SOFTC Presentation - Demand management using workflow Project Server 2010Ordina SOFTC Presentation - Demand management using workflow Project Server 2010
Ordina SOFTC Presentation - Demand management using workflow Project Server 2010
 
Ordina SOFTC Presentation - Demand management using workflow Project Server 2010
Ordina SOFTC Presentation - Demand management using workflow Project Server 2010Ordina 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 - Bridging the project and work management gapOrdina SOFTC Presentation - Bridging the project and work management gap
Ordina SOFTC Presentation - Bridging the project and work management gap
 
Ordina SOFTC Presentation - Visual Studio LightSwitch
Ordina SOFTC Presentation - Visual Studio LightSwitchOrdina SOFTC Presentation - Visual Studio LightSwitch
Ordina SOFTC Presentation - Visual Studio LightSwitch
 
Ordina SOFTC Presentation - Building a WP7 app - Lessons learned
Ordina SOFTC Presentation - Building a WP7 app - Lessons learnedOrdina SOFTC Presentation - Building a WP7 app - Lessons learned
Ordina SOFTC Presentation - Building a WP7 app - Lessons learned
 
Ordina SOFTC Presentation - Windows Phone 7 and the cloud
Ordina SOFTC Presentation - Windows Phone 7 and the cloudOrdina SOFTC Presentation - Windows Phone 7 and the cloud
Ordina SOFTC Presentation - Windows Phone 7 and the cloud
 
Ordina SOFTC Presentation - Moving a legacy application into the cloud
Ordina SOFTC Presentation - Moving a legacy application into the cloudOrdina SOFTC Presentation - Moving a legacy application into the cloud
Ordina SOFTC Presentation - Moving a legacy application into the cloud
 
Ordina SOFTC Presentation - Deployment with TFS Build and Workflow
Ordina SOFTC Presentation - Deployment with TFS Build and WorkflowOrdina SOFTC Presentation - Deployment with TFS Build and Workflow
Ordina SOFTC Presentation - Deployment with TFS Build and Workflow
 

Async CTP Overview - Unifying Asynchrony with Tasks

  • 2. .NET Software engineer Email kristof@kristofmattei.be kristof.mattei@ordina.be Website www.kristofmattei.be Twitter @Snakiej
  • 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; }
  • 10. Old vs new demo
  • 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

  1. UI that is blocking while downloading something-&gt; Some browser window waits for an operation -&gt; annoyingMultiple CPUs -&gt; utilise that power
  2. Point out keywords0-Go
  3. TitleGrabber