C# 异步委托回调函数使用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncDelegateExam
{
delegate int TakeAWhileDelegate(int data,int ms);
class Program
{
static void Main(string[] args)
{
TakeAWhileDelegate del = TakeAWhile;
del.BeginInvoke(1, 1000,
ar => //委托执行完后执行回调函数
{
int result = del.EndInvoke(ar);
Console.WriteLine("result:{0}", result);
}, null);
for (int i = 0; i < 100; i++)
{
Console.WriteLine(".");
Thread.Sleep(50);
}
Console.Read();
}
static int TakeAWhile(int data,int ms)
{
Console.WriteLine("TakeAWhile Started!");
Thread.Sleep(ms);
Console.WriteLine("TakeAWhile Completed!");
return ++data;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncDelegateExam
{
delegate int TakeAWhileDelegate(int data,int ms);
class Program
{
static void Main(string[] args)
{
TakeAWhileDelegate del = TakeAWhile;
del.BeginInvoke(1, 1000,
ar => //委托执行完后执行回调函数
{
int result = del.EndInvoke(ar);
Console.WriteLine("result:{0}", result);
}, null);
for (int i = 0; i < 100; i++)
{
Console.WriteLine(".");
Thread.Sleep(50);
}
Console.Read();
}
static int TakeAWhile(int data,int ms)
{
Console.WriteLine("TakeAWhile Started!");
Thread.Sleep(ms);
Console.WriteLine("TakeAWhile Completed!");
return ++data;
}
}
}