using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
/// <summary>
/// 定义异步委托
/// </summary>
public delegate void ExeDelegate();
static void Main(string[] args)
{
Console.WriteLine("程序开始执行...");
ExeDelegate ed = new ExeDelegate(Add);
//开始执行异步
IAsyncResult result = ed.BeginInvoke(CallBack, "异步结束");
Thread.Sleep(2000);
Console.WriteLine("程序正在运行...");
Thread.Sleep(8000);
Console.WriteLine("程序运行结束...");
}
/// <summary>
/// 异步执行的方法
/// </summary>
public static void Add()
{
Thread.Sleep(6000);
int a = 2;
int b = 3;
Console.WriteLine(a + b);
}
/// <summary>
/// 异步回调函数
/// </summary>
/// <param name="result"></param>
public static void CallBack(IAsyncResult result)
{
ExeDelegate ed = (ExeDelegate)((AsyncResult)result).AsyncDelegate;
ed.EndInvoke(result);
Console.WriteLine(result.AsyncState);
}
}
}