public class TestFunc { private string Uname = "joyet"; private int Num1 = 109; private int Num2 = 5; private int Num3 = 0; /// <summary> /// 测试同步方法 /// </summary> public void TestYncMethod() { Console.WriteLine("同步Add方法开始====="); Func<int,int,int> func = TestMethods.Add; func.Invoke(Num1, Num2); Console.WriteLine("同步Add方法结束=====。"); } /// <summary> /// 测试异步方法 /// </summary> public void TestAsyncPlus() { Console.WriteLine("异步Add方法开始====="); Func<int,int,int> func = TestMethods.Add; IAsyncResult result = func.BeginInvoke(Num1, Num2, new AsyncCallback(TestAsyncPlusCallBack), null); Console.WriteLine("异步Add方法结束=====。"); } /// <summary> /// 异步回调用回调函数,当调用结束时会自动调用回调函数,解决了为等待调用结果,而让线程依旧被阻塞的局面。 /// </summary> /// <param name="result"></param> private void TestAsyncPlusCallBack(IAsyncResult result) { Func<int, int, int> f = (Func<int, int, int>)((AsyncResult)result).AsyncDelegate; Num3 = f.EndInvoke(result); Console.WriteLine("异步Add方法相加结果:" + Convert.ToString(Num3)); } }