class Program
{
public delegate int dele_async(int a);
static void Main(string[] args)
{
dele_async dele_method = new dele_async(Executefunc);
//委托方法实例化以后有Invoke和BeginInvoke方法,Invoke是同步调用,BeginInvoke是异步等待。
//int result = dele_method.Invoke(10);
IAsyncResult result = dele_method.BeginInvoke(10,asynccallback,dele_method);
Console.WriteLine("主函数执行完毕!");
Console.ReadLine();
}
private static int Executefunc(int m)
{
Console.WriteLine("正在执行invoke函数...");
Thread.Sleep(2000);
Console.WriteLine("执行invoke函数完毕");
return m * m;
}
private static void asynccallback(IAsyncResult iar)
{
Console.WriteLine("正在执行callback函数...");
dele_async dele_method = (dele_async)iar.AsyncState;
int result = dele_method.EndInvoke(iar);
Console.WriteLine(result);
Console.WriteLine("执行callback函数完毕");
}
}
C#回调函数callback使用方法 - 简书 (jianshu.com)https://www.jianshu.com/p/71154647a49d