Async Delegates in C#

This blog shows on how to use an Async Delegates in C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace Demo
{
    class Program
    {
        static int TakesAWhile(int data, int ms)
        {
            Console.WriteLine("TakesAWhile started");
            Thread.Sleep(ms);
            Console.WriteLine("TakesAWhile completed");
            return ++data;
        }

        public delegate int TakesAWhileDelegate(int data, int ms);

        static void Main()
        {            
            TakesAWhileDelegate d1 = TakesAWhile;
            d1.BeginInvoke(1, 3000,
               ar =>
               {
                   int result = d1.EndInvoke(ar);
                   Console.WriteLine("result: {0}", result);
               },
               null);
            for (int i = 0; i < 100; i++)
            {
                Console.Write(".");
                Thread.Sleep(50);
            }

        }

        static void TakesAWhileCompleted(IAsyncResult ar)
        {
            if (ar == null) throw new ArgumentNullException("ar");

            TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate;
            Trace.Assert(d1 != null, "Invalid");

            int intresult = d1.EndInvoke(ar);
            Console.WriteLine("result: {0}", intresult);
        }
    }
}
Next Recommended Reading Delegates and Types of Delegates