• Save
Ordina SOFTC Presentation - Async CTP
Upcoming SlideShare
Loading in...5
×
 

Ordina SOFTC Presentation - Async CTP

on

  • 1,088 views

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

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

Statistics

Views

Total Views
1,088
Views on SlideShare
1,088
Embed Views
0

Actions

Likes
1
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • UI that is blocking while downloading something-> Some browser window waits for an operation -> annoyingMultiple CPUs -> utilise that power
  • Point out keywords0-Go
  • TitleGrabber

Ordina SOFTC Presentation - Async CTP Ordina SOFTC Presentation - Async CTP Presentation Transcript

  • 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