Asynchronous method calls
Using delegates, how can we make Asynchronous method calls?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Veera ChennaPosted Mar 27, 2012, 6:10 PM
//declare a delegate
public delegate DataSet AsyncCaller();
//instantiate the delegate
AsyncCaller asyncCaller = new AsyncCaller(DataManager.RetrieveClients);
//call the method asynchronously
IAsyncResult result = asyncCaller.BeginInvoke(RetrieveAyncCallBack, null);
public void RetrieveAyncCallBack(IAsyncResult result){
// Extract the delegate from the System.Runtime.Remoting.Messaging.AsyncResult.
AsyncCaller asyncCaller = (AsyncCaller)((AsyncResult)result).AsyncDelegate;
// Obtain the result.
object obj = asyncCaller.EndInvoke(result);
}
Sam HobbsPosted Mar 26, 2012, 1:48 PM
Jignesh TrivediPosted Mar 26, 2012, 4:01 AM
Delegates enable you to call method synchronous as well as asynchronous manner.
The common language runtime(CLR) automatically defines BeginInvoke and EndInvoke methods for the delegate, with the appropriate signatures.
Example
public delegate int AddDelegate(int a, int b);
public static int Add(int a, int b)
{
return a + b;
}
AddDelegate a = new AddDelegate(Add);
IAsyncResult async = a.BeginInvoke(5, 10, null, null);
int result = a.EndInvoke(async);
hope this help.
SenthilkumarPosted Mar 26, 2012, 12:44 AM
An useful feature of delegates is the ability to execute a method asynchronously. That is, through a delegate, you can begin invocation of a method and then return immediately while the delegate executes its method in a separate thread.
I suggest you to go through these urls:
http://www.dotnetcurry.com/ShowArticle.aspx?ID=634
http://msdn.microsoft.com/en-us/magazine/cc301332.aspx
http://www.expertsupdates.com/csharp-tutorials/asynchronous-method-calls-using-delegates-16.aspx