class Program
{
public delegate int SumCallback(int x, int y);
public int Sum(int x, int y)
{
Console.WriteLine("等待...");
System.Threading.Thread.Sleep(2000);
return x + y;
}
public void Callback(IAsyncResult ar)
{
Console.WriteLine("回调...");
AsyncResult result = ar as AsyncResult;
if (result!=null)
{
SumCallback caller = (SumCallback)result.AsyncDelegate;
Console.WriteLine(caller.EndInvoke(result));
}
}
static void Main(string[] args)
{
SumCallback sc = new Program().Sum;
IAsyncResult result = sc.BeginInvoke(1, 2,new Program().Callback, sc);
Console.WriteLine("主线程...");
Console.ReadKey(true);
}
}