Freigeben über


A technical introduction to the Async CTP

I made this slide deck that covers all of the Async CTP technically.

Here are its contents:

  • [100-level] Straightforward explanation of when and how to use Async
    • What is the difference between "asynchrony" and "concurrency" -- and when to use them (and when not)
    • Why asynchrony is different from "running on a background thread"
    • How to use the async+await keywords
  • [200-level] How exactly does an async program behave?
    • A detailed animation of control-flow of an async method
    • What happens on the UI thread, IOCP (IO Completion Port) threads, and the UI message-queue
    • What is the new "Task Asynchronous Pattern" (TAP) in the framework
    • How to use cancellation in the TAP, and push or pull approaches to progress-notification
  • [300-level] What library-authors should know about async
    • How to make your own types awaitable
    • How to implement TAP-like APIs yourself
  • [400-level] How is async implemented? How was it designed?
    • Full details of the compiler transformation -- how it rewrites asynchronous methods, and how it deals with try/catch blocks
    • Discussion and explanation of several design decisions we made
    • How Async relates to existing computer science theory.

 

https://blogs.msdn.com/cfs-file.ashx/__key/CommunityServer-Blogs-Components-WeblogFiles/00-00-01-12-06/4861.Technical-intro-to-the-Async-CTP.pptx

Comments

  • Anonymous
    October 29, 2010
    Thanks as always for your detailed, well-thought out explainations. It gives the subject good clarity.

  • Anonymous
    November 01, 2010
    Is cancellation supported with TaskEx.Run method? Consider the following code:    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();        }        private CancellationTokenSource cancelationRoute;        private async void Button_Click(object sender, RoutedEventArgs e)        {            var btn = sender as Button;            if(btn.Name=="btnDoWork")            {                btnDoWork.IsEnabled = false;                lblResult.Content = string.Empty;                await DoWork();            }            else if(btn.Name == "btnCancel")            {                cancelationRoute.Cancel();            }        }        private async Task DoWork()        {            Task<string> task;           try           {               cancelationRoute = new CancellationTokenSource();               task = TaskEx.Run<string>(CopyFile, cancelationRoute.Token);               await task;               lblResult.Content = task.Status.ToString();               btnDoWork.IsEnabled = true;           }           catch(OperationCanceledException)           {               lblResult.Content = "canceled";               btnDoWork.IsEnabled = true;               return;           }           cancelationRoute = null;        }        private string CopyFile()        {            Enumerable.Repeat(100, 50).ToList().ForEach(Thread.Sleep);            return "Ok, I finished!";        }    } } This is code-behind of simple UI consists of two buttons "Do Work" and "Cancel" and result label. If I click Do Work button, everything is fine - the UI is responsive and 5 seconds later the result label shows "RanToCompletion" status. But cancellation does not work. Even though I click Cancel which calls to cancelationRoute.Cancel() while the task is run, the task ends up with RanToCompletion status. Am I doing something wrong?

  • Anonymous
    November 01, 2010
    I'm pretty sure that your CopyFile() method needs to check the cancellation request somewhere. For instance the CTP sample "CancellationToken - Single CPU-bound request" does this: return TaskEx.Run(() =>    {        var result = new int[width * height];        for (int y = 0; y < height; y++)        {            cancellationToken.ThrowIfCancellationRequested();            for (int x = 0; x < width; x++)            {                Thread.Sleep(10);   // simulate processing cell [x,y]            }            Console.WriteLine("Processed row {0}", y);        }        return result;    }); So in it's loop it checks the cancalleation request every time round.

  • Anonymous
    November 02, 2010
    @Matt Warren thank you! This is what I obviously missed. The methods like WebClient.DownloadFileTaskAsync throw if cancellation is requested and we need to do it too in TaskEx.Run methods. Thanks!

  • Anonymous
    November 02, 2010
    @SergeyA Yeah it's up to the method to decide how to handle the cancellation, it's not done automatically for you.

  • Anonymous
    January 13, 2011
    Why are the first couple pages in the slides written in VB rather than C#? yet the slverlight port later was done in C#. This presentation won't please either VB or C# programmers. For heaven's sake, stick to a single language (C#) in the same slide and translate it to a different language if there are enough VBers crying out for it,

  • Anonymous
    March 25, 2011
    There needs to be VB slides for all presentations!

  • Anonymous
    July 25, 2013
    good technical methode

  • Anonymous
    October 16, 2017
    Can I see the actual talk somewhere?