Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Asynchronous Nature of Delegates

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 272
    Comment on it

    Asynchronous Nature of Delegates

     

    Delegates are defined as function pointer i.e act as a pointer to methods in the application.

     

    Example of Delegate :-

     

    using System;
    
    namespace ConsoleApplication2
    {
        public delegate int DemoDelegate(int x);   //delegate declaration
        public class Demo
        {
            public int Square(int x)     //A method to be invoked by the delegate
            {
                return x * x;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Demo demo = new Demo();
                DemoDelegate del = new DemoDelegate(demo.Square);
                int result = del(9);  //method called synchronously
    
                //all the lines below will not be executed till above operation is finished
    
                Console.WriteLine("Proccessing Square operation...");
                Console.WriteLine("Result is: {0}", result);
                Console.ReadKey();
            }
        }
    }

     

    Output :

     

     

    In above program delegate is invoking the "Square" method in main thread of the application but the application will become unresponsive in case a time taking process is to be executed when method was invoked.

     

    Like if Square method is changed to:-

     

    
     public int Square(int x)     //A method to be invoked by the delegate
     {
       Thread.Sleep(20000);
       return x * x;
     }

     

    Therefore whenever above method is called via delegate will take about 20 second to complete,at that time application will not respond. The output will be generated after 20 seconds.

     

    This problem can be solved by using the delegate to call method asynchronously.

     

    Example to demonstrate asynchronous nature of Delegates :-

     

    using System;
    using System.Threading;
    
    namespace ConsoleApplication2
    {
        public delegate int DemoDelegate(int x);
        public class Demo
        {
            //A method to be invoke by the delegate
            public int Square(int x)
            {
                Thread.Sleep(10000);  //time taking process
                return x * x;
            }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Demo demo = new Demo();
                DemoDelegate del = new DemoDelegate(demo.Square);
    
                //Invoke "Square" method in another thread earlier called in main thread
                IAsyncResult async = del.BeginInvoke(9, null, null);
                
                //anything can be done while Square method is executing.
    
                Console.WriteLine("Proccessing Square operation...");
                int result = del.EndInvoke(async);
                Console.WriteLine("Result is: {0}", result);
                Console.ReadKey();
            }
        }
    }


     

    Whenever a delegate is defined a custom delegate is created which includes various methods like Invoke(), BeginInvoke() and EndInvoke(). Whenever delegate calls a method actually Invoke() method is being called which is making a synchronous call to the method i.e executing in  main thread only. To make asynchronous call to method by using delegate use BeginInvoke() method. Whenever BeginInvoke() method is called via delegate, the method to which delegate is pointing will be queued to start on other thread. BeginInvoke() method  returns IAsyncResult object which can be used for determining when the asynchronous operation is complete. To obtain results IAsyncResult object is submitted to EndInvoke() method of the delegate. EndInvoke() method will wait for the asynchronous operation to complete if it is not complete and then a return value is provided.

     

    BeginInvoke() method has three parameters, first parameter are all the parameters to be passed to method to which delegate is pointing,second parameter used for callback and third parameter is state object. Pass null if 2nd and 3rd parameter are not required.

     

    In above program output will remain same but the line "Proccessing Square operation..." will be written to console as soon as program will run need not to wait for 20 seconds whereas "Square" method will take about 20 second to complete.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: