C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Code Snippets » Visual Studio »

Asynchronous Execution of a method using Delegates and Call-back method


Posted Date: 09 Dec 2007    Resource Type: Code Snippets    Category: Visual Studio
Author: Balamurali BalajiMember Level: Diamond    
Rating: 1 out of 5Points: 10



The following code shows how to execute a method asynchronously using delegates and how a call-back method is executed when it is completed. As you run the code, you can see the Add method displays the sum of two numbers and wait for 3 seconds. Only then, the method call ends and executes the code in the addCompleted method.



delegate void deleType(int x, int y);
public class Class1
{
public void Add(int i, int j)
{
Console.WriteLine(i + j);
System.Threading.Thread.Sleep(3000);
}
public void addCompleted(IAsyncResult result)
{
deleType deleInstance = (deleType) result.AsyncState;
deleInstance.EndInvoke(result);
if (result.IsCompleted == true)
Console.WriteLine("Asynchronous call completed!");
}
public static void Main()
{
Class1 c = new Class1();
deleType deleInstance = new deleType(c.Add);
AsyncCallback cbackMethod = new AsyncCallback(c.addCompleted);
IAsyncResult result = deleInstance.BeginInvoke(10, 20, cbackMethod, deleInstance);
Console.WriteLine("Press Enter to close application.");
Console.ReadLine();
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
Method using Delegates  .  Execution  .  Asynchronous  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Reversing a string
Previous Resource: Retrieving Files from TFS programatically
Return to Discussion Resource Index
Post New Resource
Category: Visual Studio


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use