Asynchronous method calls in c# [part 4] — Call back
Atlast, callback. Now, whats call back?? Remember part 1 of this post??
Callback is the techinique of executing a method automatically, when the asynchronous call has been finished.
Lets see how we can do this.
For this tutorial, we need to change the functionalities a bit. The following changes needs to be done.
RemoteObject.cs file
The signature of the method, GetMessage() should be changed in such a way that it takes a argument and gives out an out parameter.
public void GetMessage(string theName, out string theMesssage) //The method returns an out parameter instead of a message box.
// MessageBox will be shown in the form1.cs file
{
Thread.Sleep(10000);
theMessage = "Hello " + thename;
}
Form1.cs file
The signature of the AsyncCaller delegated should be changed to,
private delegate void AsyncCaller(string theName, out string theMessage); //Now the method returns an out parameter
The way the BeginInvoke() is called needs to be altered,
aAsyncCaller.BeginInvoke("foo", myMessage, new AsyncCallback(ShowMessage), null); //"foo" is the parameter, myMessage is a class level field variable for out parameter and AsyncCallback(...) is the way in which the csc will be notified with the method that it should call once the asynchronous method has completed its execution
Addition of a new method, ShowMessage that takes an IAsyncResult as its parameter
private void ShowMessage(IAsyncResult theAsyncResult) //This is the callback method and will show the message box with the out parameter's value
{
AsyncResult aAsyncResult = theAsyncResult as AsyncResult; //Creating a AsyncResult object from the interface parameter. This paramater actually holds out intended result
AsyncCaller aAsyncCaller = aAsyncRsult.AsyncDelegate; //Getting the delegate that was used to call the method asynchronously
aAsyncCaller.EndInvoke(out myMessage, aAsyncResult); //Getting the callback message from the delegate
MessageBox.Show(myMessage); //Shows the message box to the user with the message that he has got from the RemoteObject.cs file
}
Insight of BeginInvoke() and ShowMessage()
- As you have noticed the BeginInvoke() method has a big change. the first parameter, “foo” is the parameter for the GetMessage() method, the second parameter is the out parameter, while the third parameter new AsyncCallback(ShowMessage) is the way by which you are telling the csc (c# compiler) that ShowMessage is the method that will be called after the execution of GetMessage. The constructor of the AsyncCallback class takes the callback method name.
- ShowMessage(IAsyncResult) is a new method. It takes an IAsyncResult parameter. IAsyncResult is an interface. The object that it receives as parameter will hold all the details about the asychronous call. It has the result of the call, the delegate that was used to make the call and many more.
- We actually get the result of the call using the delegate that was used to make the call. So the first statement in this method would be the typecasting of the interface object to AsyncResult object.
- Secondly, we need to get the delegate that was used to make the asynchronous call (it will be AsyncCaller). Thats what we have done in the second statement.
- Third statement ends the call by calling the EndInvoke() to get the result of the GetMessage().
This is a simple method of implementing callbacks in c#. Hope you understood. There are loads of other ways in whihc you can accomplish this. I will try to post the alternative ways very soon.

