Asynchronous programming with delegates and CallBack


Asynchronous programming with delegate and CallBack
 
using System;
using System.Runtime.Remoting.Messaging;
using System.Threading;

namespace Memory_understanding
{

    public delegate int myfundelegate(int num);
    class myMethod
    {
        public int myfun(int a)
        {
            Thread.Sleep(10000);
            return a*a;

        }
    }

    class program
    {
        public static void Main()
        {
            myMethod a = new myMethod();
           
            myfundelegate d = new myfundelegate(a.myfun);
            IAsyncResult async = d.BeginInvoke(25, new AsyncCallback(mycallBackAsyn), "This from Main Thread");
            Console.WriteLine("Processing the operations");
            Console.ReadLine();
        }

        private static void mycallBackAsyn(IAsyncResult ar)
        {
            AsyncResult a = (AsyncResult)ar;
            myfundelegate del = (myfundelegate)a.AsyncDelegate;
            int result = del.EndInvoke(ar);

            string msg = (string)ar.AsyncState;
            Console.WriteLine("{0} : the result is {1}", msg, result);
        }


    }


}

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6