Patrick
Patrick

Reputation: 884

Async Delegates Callback Pattern leveraging Func

All -

I have a sample code for Async Delegates which utilizes callback pattern. I am using the standard delegates and the code below works fine. Wanted to convert it to Func delegates (since its expecting and int and returning an int) but for some reason - seem to get stuck. Can somebody please help.

class Program
{
    public delegate int SomeDelegate(int x);

    static void Main(string[] args)
    {
        Console.WriteLine("This is the CallBack Pattern Technique for Asynchronous Delegates in Threading");
        Console.WriteLine("");
        SomeDelegate sd = SquareNumber;
        Console.WriteLine("[{0}] Before SquareNumber Method invoked.", Thread.CurrentThread.ManagedThreadId);
        IAsyncResult asyncRes = sd.BeginInvoke(10, new AsyncCallback(CallbackMethod), null);
        Console.WriteLine("[{0}] Back in Main after SquareNumber Method invoked. Doing Extra Processing.", Thread.CurrentThread.ManagedThreadId);
        Console.WriteLine("[{0}] Main method processing completed.", Thread.CurrentThread.ManagedThreadId);
        Console.ReadLine();
    }

    static int SquareNumber(int a)
    {
        Console.WriteLine("[{0}] Inside SquareNumber  - invoked. Processing......", Thread.CurrentThread.ManagedThreadId);
        Thread.Sleep(5000);
        return a * a;
    }

    static void CallbackMethod(IAsyncResult asyncRes)
    {
        Console.WriteLine("[{0}] Callback Invoked", Thread.CurrentThread.ManagedThreadId);
        AsyncResult ares = (AsyncResult)asyncRes;
        SomeDelegate delg = (SomeDelegate)ares.AsyncDelegate;
        int res = delg.EndInvoke(asyncRes);
        Console.WriteLine("[{1}] Result = {0}", res, Thread.CurrentThread.ManagedThreadId);
    }
}

Thanks

Upvotes: 0

Views: 1302

Answers (1)

TheKingDave
TheKingDave

Reputation: 976

class Program
    {
        //public delegate int SomeDelegate(int x);

        static void Main(string[] args)
        {
            Console.WriteLine("This is the CallBack Pattern Technique for Asynchronous Delegates in Threading");
            Console.WriteLine("");
            Func<int,int> sd = SquareNumber;
            Console.WriteLine("[{0}] Before SquareNumber Method invoked.", Thread.CurrentThread.ManagedThreadId);
            IAsyncResult asyncRes = sd.BeginInvoke(10, new AsyncCallback(CallbackMethod), null);
            Console.WriteLine("[{0}] Back in Main after SquareNumber Method invoked. Doing Extra Processing.", Thread.CurrentThread.ManagedThreadId);
            Console.WriteLine("[{0}] Main method processing completed.", Thread.CurrentThread.ManagedThreadId);
            Console.ReadLine();
        }

        static int SquareNumber(int a)
        {
            Console.WriteLine("[{0}] Inside SquareNumber  - invoked. Processing......", Thread.CurrentThread.ManagedThreadId);
            Thread.Sleep(5000);
            return a * a;
        }

        static void CallbackMethod(IAsyncResult asyncRes)
        {
            Console.WriteLine("[{0}] Callback Invoked", Thread.CurrentThread.ManagedThreadId);
            AsyncResult ares = (AsyncResult)asyncRes;
            Func<int,int> delg = (Func<int,int>)ares.AsyncDelegate;
            int res = delg.EndInvoke(asyncRes);
            Console.WriteLine("[{1}] Result = {0}", res, Thread.CurrentThread.ManagedThreadId);
        }
    }

Works for me.

Upvotes: 1

Related Questions