Archive for the ‘Delegates’ tag
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.
Asynchronous method calls in c# [part 3] — using parameters in calls
Sorry for the delay in part 3. Without wasting time, lets get strated. Till part 2, we were using asynchronous calls with out any parameters. Now in this one, we will try to pass some parameters to the GetMessage method. Here also we are gonne consider the same example. If you haven’t read the part 2 of this article, go thru’ it before beginning this one.
Modifications to be done in GetMessage method
In the RemoteObject.cs file, modify the signature of the GetMessage class by making it accepting a string parameter. Consider that the GetMessage method now looks some thing like this,
public void GetMessage(string theName)
{
MessageBox.Show(theName);
}
Modifications to be done in the Form1.cs file
In the Form1.cs file, there are a couple of modofications to be done. First, we need to change the delegate’s signature.
private delegate void AsyncCaller(string theName); //Now the delegate acceps a parameter
Also in the button’s click event, the way we invoke the delegate method needs to be modified.
Instead of just calling aAsyncCaller.BeginInvoke(null, null); this time, we will call it using
aAsyncCaller.BeginInvoke("foo", null, null); //"foo" is the parameter to the GetMessage() method
Thats it. you have made an async call passing some arguments. Try to run it. Clicking the button should give you a message box with the string “foo“.
Next up, Callbacks.
Still confused with null, null in the BeginInvoke() method call??
You will get the answer in my next post.

