in .Net, C#

Asynchronously run a method with the Async and Await keywords

I wrote this small class to illustrate the use of the Async and Await keywords which were added in .NET 4.5.
It should quickly help anyone curious to understand this easier way to write asynchronous programs in C#.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Synchronous method");
            Console.WriteLine("Before calling DisplaySum");
            DisplaySum();
            Console.WriteLine("After calling DisplaySum");
            Console.WriteLine("Press a key to continue.");
            Console.ReadKey();

            Console.WriteLine("************************");
            Console.WriteLine("Asynchronous method");

            Console.WriteLine("Before calling DisplaySumAsync");
            DisplaySumAsync();
            Console.WriteLine("After calling DisplaySumAsync");
            Console.WriteLine("Press a key to quit.");
            Console.ReadKey();
        }

        public static double Calculate()
        {
            Console.WriteLine("Method Calculate()");
            double x = 1;
            // Long-running method
            for (int i = 1; i < 100000000; i++)
            {
                x += Math.Tan(x) / i;
            }
            return x;
        }

        // Synchronous method
        private static void DisplaySum()
        {
            Console.WriteLine("Method DisplaySum()");
            double result = Calculate();
            Console.WriteLine("DisplaySum DisplaySum - result : " + result);
        }

        // ***************************************

        public static Task<double> CalculateAsync()
        {
            Console.WriteLine("Method CalculateAsync()");
            return Task.Run(() =>
            {
                double x = 1;
                // Long-running method
                for (int i = 1; i < 100000000; i++)
                {
                    x += Math.Tan(x) / i;
                }
                return x;
            });
        }

        // Asynchronous method
        private static async void DisplaySumAsync()
        {
            Console.WriteLine("Method DisplaySumAsync()");
            double result = await CalculateAsync();
            Console.WriteLine("Method DisplaySumAsync - result : " + result);
        }
    }
}

The output below (DOS window) shows clearly the difference between the call of a synchronous method and the call of an asynchronous method.
The message “After calling DisplaySumAsync” (line 25) is called immediately after the call of the DisplaySumAsync() method, whether it is finished or not.
The important thing to note is : the lines written in the same thread used to call the asynchronous method (which runs in another thread) are executed immediately. An asynchronous method does not block the calling function, allowing that function to continue functioning.

AsyncAwait