TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Async Delegates in C#
Karthikeyan Anbarasan
Apr 01, 2011
4.2
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
How to use the Async Delegates in C#.
This blog shows on how to use an Async Delegates in C#
using System;
using
System
.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;
namespace Demo
{
class Program
{
static int TakesAWhile(int data, int ms)
{
Console.WriteLine("TakesAWhile started");
Thread.Sleep(ms);
Console.WriteLine("TakesAWhile completed");
return ++data;
}
public delegate int TakesAWhileDelegate(int data, int ms);
static void Main()
{
TakesAWhileDelegate d1 = TakesAWhile;
d1.BeginInvoke(1, 3000,
ar =>
{
int result = d1.EndInvoke(ar);
Console.WriteLine("result: {0}", result);
},
null);
for (int i = 0; i < 100; i++)
{
Console.Write(".");
Thread.Sleep(50);
}
}
static void TakesAWhileCompleted(IAsyncResult ar)
{
if (ar == null) throw new ArgumentNullException("ar");
TakesAWhileDelegate d1 = ar.AsyncState as TakesAWhileDelegate;
Trace.Assert(d1 != null, "Invalid");
int intresult = d1.EndInvoke(ar);
Console.WriteLine("result: {0}", intresult);
}
}
}
Async Delegates
Next Recommended Reading
Delegates and Types of Delegates