Codelog

foreach(Snippet aSnippet in CodeLog){ aSnippet.GetSolution(); }

Asynchronous method calls in c# [part 3] — using parameters in calls

without comments

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.

Written by sudarsanyes

October 3rd, 2008 at 9:04 am

Posted in C#, Tips 'n' Tricks, UI

Tagged with , , ,

Leave a Reply