委托异步请求
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Runtime.Remoting.Messaging;
namespace LearnConsoleApp
{
public delegate int BinaryOp(int x, int y);
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Main() invoked on thread{0}",Thread.CurrentThread.GetHashCode());
BinaryOp b = new BinaryOp(Add);
IAsyncResult ret = b.BeginInvoke(3, 2, new AsyncCallback(AddComplete), "Success,Success!!!!!!I'm very happy!");
int i = 0;
while (i<10)
{
i++;
Console.WriteLine("Doing more work in Main(),Current thread is thread{0}", Thread.CurrentThread.GetHashCode());
}
Console.ReadLine();
}
static int Add(int a, int b)
{
Console.WriteLine("Main() invoked on thread{0}", Thread.CurrentThread.GetHashCode());
Thread.Sleep(5000);
return a + b;
}
static void AddComplete(IAsyncResult itfAr)
{
Console.WriteLine("AddComplete() invoked on thread {0}",Thread.CurrentThread.GetHashCode());
Console.WriteLine("Your addition is complete.");
BinaryOp op = (BinaryOp)((AsyncResult)itfAr).AsyncDelegate;
int answer = op.EndInvoke(itfAr);
Console.WriteLine("3 + 2 is {0},{1}", answer,itfAr.AsyncState.ToString());
}
}
}