1.5.3.1. fejezet, Aszinkron delegáltak

namespace AsyncCallback
{
    class Program
    {
        public delegate int MyDelegate(int x);
        static int Square(int x)
        {
            Console.WriteLine("Thread-ID: {0}", Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(100);
            return (x * x);
        }
 
        static void AsyncMethodComplete(IAsyncResult iar)
        {
            Console.WriteLine("Aszinkron szál kész...");
            AsyncResult result = (AsyncResult)iar;
            MyDelegate d = (MyDelegate)result.AsyncDelegate;
            Console.WriteLine("Alap: {0}", iar.AsyncState);
            Console.WriteLine("Eredmény: {0}", d.EndInvoke(iar));
        }
 
        static void Main(string[] args)
        {
            MyDelegate d = Square;
            Console.WriteLine("Thread-Id: {0}", Thread.CurrentThread.ManagedThreadId);
            int ibase = 12;
            IAsyncResult iar = d.BeginInvoke(ibase, null, null);
            while (!iar.IsCompleted)
            {
                Console.WriteLine("BlaBla...");
            }
            int result = d.EndInvoke(iar);
            Console.WriteLine("Square({0}): {1}",ibase,result);
 
            iar = d.BeginInvoke(ibase, new AsyncCallback(AsyncMethodComplete),ibase);
            Console.WriteLine("BlaBla...");
            Console.ReadKey();
        }
    }
}