Asynchronous Programming Using Delegates
Delegated allow you to call any synchronous method asynchronously. In this post I will discuss way of doing this.
BeginInvoke() :
When a delegate is called using BeginInvoke it has same parameters as the method that you want to execute and two other parameters.
- AsyncCallback :delegate that references a method to be called when the asynchronous call completes
- Sender Object: a user-defined object that passes information into the callback method
lets see how this could be done step by step.
Step 1: Define delegate & call it asynchronously
As the first step you can do the followings.
- Define a delegate with same signature as the method to be called asynchronously
- Assign the method to be called asynchronously to the delegate
- Invoke the delegate asynchronously
See the sample code here:
- //Define a delegate with same signature as the method to be called asynchronously
- private delegate void UpdateWorkLogHandler(int hours, string workType);
- static void Main(string[] args)
- {
- DoWork(10, "Development");
- }
- public static void DoWork(int hours, string workType)
- {
- //Assign the method to be called asynchronously to the delegate
- UpdateWorkLogHandler updateWorkLogHandler = UpdateWorkLog;
- //Invoke the delegate asynchronously
- updateWorkLogHandler.BeginInvoke(hours, workType, null, null);
- Console.WriteLine("Work Completed!. Log updating in progress");
- Console.ReadLine();
- }
- public static void UpdateWorkLog(int hours, string workType)
- {
- System.Threading.Thread.Sleep(1000);
- Console.WriteLine("Work log updated!");
- Console.ReadLine();
- }
Output Window :
You can see here before completion of UpdateWorkLog method, programs has printed the “Work Completed!. Log updating in progress”. It is because as UpdateWorkLog is called asynchronously rest of the code in DoWork method is been executed. If you have invoked the delegate without BeginInvoke output will be the following.
Step 2: With Callback
In case if you want to know whether that method is executed and call back to a method which you define & then execute some other logic you can assign a callback method on BeginInvoke as below.
- //Define a delegate with same signature as the method to be called asynchronously
- private delegate void UpdateWorkLogHandler(int hours, string workType);
- static void Main(string[] args)
- {
- DoWork(10, "Development");
- }
- public static void DoWork(int hours, string workType)
- {
- //Assign the method to be called asynchronously to the delegate
- UpdateWorkLogHandler updateWorkLogHandler = UpdateWorkLog;
- //Invoke the delegate asynchronously
- updateWorkLogHandler.BeginInvoke(hours, workType, UpdateWorkLogCompleted, null);
- Console.WriteLine("Work Completed!. Log updating in progress");
- Console.ReadLine();
- }
- //Callback Method
- public static void UpdateWorkLogCompleted(IAsyncResult ar)
- {
- Console.WriteLine("Work log updated Callback called");
- Console.ReadLine();
- }
- public static void UpdateWorkLog(int hours, string workType)
- {
- System.Threading.Thread.Sleep(1000);
- Console.WriteLine("Work log updated");
- }
Output Window:
Step 3: With EndInvoke
In case if you need a return value from the asynchronously called method you can use EndInvoke.
The EndInvoke method retrieves the results of the asynchronous call. It can be called any time after BeginInvoke. If the asynchronous call has not completed, EndInvoke blocks the calling thread until it completes.
See the sample code below. It take the return value and then do the rest of the execution.
//Define a delegate with same signature as the method to be called asynchronously
private delegate bool UpdateWorkLogHandler(int hours, string workType);
static void Main(string[] args)
{
DoWork(10, "Development");
}
public static void DoWork(int hours, string workType)
{
//Assign the method to be called asynchronously to the delegate
UpdateWorkLogHandler updateWorkLogHandler = UpdateWorkLog;
//Invoke the delegate asynchronously
IAsyncResult result= updateWorkLogHandler.BeginInvoke(hours, workType,null,null);
Console.WriteLine("Work Completed!. Log updating in progress");
// Call EndInvoke and get the returned value
bool returnedValue = updateWorkLogHandler.EndInvoke(result);
UpdateWorkLogCompleted(returnedValue);
Console.WriteLine("Do Work Completed");
Console.ReadLine();
}
//Callback Method
public static void UpdateWorkLogCompleted(bool result)
{
Console.WriteLine("Work log updated Callback called Result:"+ result);
}
public static bool UpdateWorkLog(int hours, string workType)
{
System.Threading.Thread.Sleep(1000);
Console.WriteLine("Work log updated");
return true;
}
Output Window :
So can see here it has received the return value. Also the it did not execute the rest of the code until after EndInvoke until it received the returned value.
Hope this helps you.
Happy Coding !!!!!
Comments
Post a Comment