using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Remoting.Messaging; using System.Text; using System.Threading;... ...
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); } } }