Func async

weixin_30924079發表於2020-04-04

class Program
{

static void Main(string[] args)
{

Func<int, int> num = Number;
IAsyncResult asyncRes = num.BeginInvoke(10, new AsyncCallback(CallbackMethod), null);

Console.Read();
}

static int Number(int a)
{
Thread.Sleep(5000);
return a + a;
}

static void CallbackMethod(IAsyncResult asyncRes)
{

AsyncResult ares = (AsyncResult)asyncRes;
Func<int, int> delg = (Func<int, int>)ares.AsyncDelegate;
int res = delg.EndInvoke(asyncRes);
Console.WriteLine("Result = {0}", res);
}
}

轉載於:https://www.cnblogs.com/Rannie/p/4447512.html

相關文章