Saturday, May 27, 2017

Asynchronous delegates [Calling Synchronous Methods Asynchronously]

.Net Framework provide the mechanism to execute methods asynchronously, Asynchronous delegates solve the problem of calling the method asynchronously and pass parameter and get the result in return when comlplete.

To do this we need to define a delegate , the delegate has same signature which the method has, there are two methods BeginInvoke and EndInvoke

There are following entities
BeginInvoke : The BeginInvoke method initiates the asynchronous call. It has the same parameters as the method that you want to execute asynchronously, plus two additional optional parameters. 1) AsynCallback Delegate and 2) User define object

AsynCallback Delegate  : it reference the method which is called when Asynchronous call completes
User Define
User Define object : It is passed to the callback method, which got in AsyncState of IAsyncResult

EndInvoke : It is use the get the result of Asyncronous call , it is used after BeginInvoke any time.
EndInvoke block the calling thread until Asyncronous call is completed.

IAsynResult : The BeginInvoke Return the IAsynResult, we can track the progress of asynchronous call.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsynchronousDelegates
{
    class Program
    {

        static void Main(string[] args)
        {
            Func<string, string, int> method = Sum;
            IAsyncResult asynResult = method.BeginInvoke("1", "2", null, null);
            var result = method.EndInvoke(asynResult);
            Console.WriteLine(result);          
            Console.Read();
        }

        static int Sum(string a, string b)
        {
            return Convert.ToInt32(a) + Convert.ToInt32(b);
        }
    }
}

Result : 3

Use Callback Delegate and Pass User define object


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AsynchronousDelegates
{
    class Program
    {

        static void Main(string[] args)
        {
            Func<string, string, int> method = Sum;
            IAsyncResult asynResult = method.BeginInvoke("1", "2", SumRes, method);
            var result = method.EndInvoke(asynResult);
            Console.Read();
        }

        private static void SumRes(IAsyncResult ar)
        {
            var target = (Func<string, string, int>)ar.AsyncState;
            int result = target.EndInvoke(ar);
            Console.WriteLine("sum is: " + result);
        }

        static int Sum(string a, string b)
        {
            return Convert.ToInt32(a) + Convert.ToInt32(b);
        }
    }
}

 








Reference
https://msdn.microsoft.com/en-us/library/2e08f6yc(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/jj152938(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/ms228963(v=vs.110).aspx
http://www.dotnetcurry.com/ShowArticle.aspx?ID=634 
http://www.albahari.com/threading/#_Thread_Pooling