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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
SenthilkumarRank #387 · 6.5K reputationPosted Mar 25, 2012, 8:44 PM EDT
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
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 26, 2012, 12:01 AM EDT
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.
Sam HobbsRank #107 · 20.5K reputationPosted Mar 26, 2012, 9:48 AM EDT
Veera Chenna26 reputationPosted Mar 27, 2012, 2:10 PM EDT
//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);
}