Saturday, February 6, 2016

Visualize async threading

For the following simple async-await code, DotTrace 10 shows code runs on main thread before Task ask thread pool to execute. So async is single thread with suspension.
This can also be seen in Concurrency Visualizer in VS 2015 as an VS Extension, where Begin wait in a separate swing lance but same thread id. Also execution stack is actually on main thread

    class Program
    {
        static void Main(string[] args)
        {
            (new Program()).Test();
            Console.ReadLine();
        }

        async void Test()
        {
          int i=  await LongRunCodeTask();
            Console.WriteLine("Asyn Call Return: " + i);
        }

        private Task<int> LongRunCodeTask()
        {
            return Task<int>.Run(() =>
            {
                Thread.Sleep(3000);
                return 99;
            });
        }
    }



No comments:

Post a Comment