Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Async Delegates in C#
WhatsApp
Karthikeyan Anbarasan
13y
4.4
k
0
0
25
Blog
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
Up Next
Delegates and Types of Delegates
Ebook Download
View all
DateTime in C#
Read by 1.3k people
Download Now!
Learn
View all
Membership not found