使用異步方式調用同步方法,在此我們使用異步編程模型(APM)實現
1、定義異步委托和測試方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;namespace ApmAsyncApp
{public delegate string TestMethodAsyncCaller(int callDuration, out int threadId);/// <summary>internal class AsyncDemo{/// 執行異步調用的方法/// </summary>/// <param name="callDuration"></param>/// <param name="threadId"></param>/// <returns></returns>public string TestMethod(int callDuration,out int threadId){Console.WriteLine("Test method begins:");Thread.Sleep(callDuration);threadId=Thread.CurrentThread.ManagedThreadId;return String.Format("Call time was {0}",callDuration);}}
}
2、使用 EndInvoke 等待異步調用
static void Main(string[] args){int threadId;//創建測試類AsyncDemo asyncDemo = new AsyncDemo();//創建委托TestMethodAsyncCaller caller = new TestMethodAsyncCaller(asyncDemo.TestMethod);//初始化異步調用IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);Thread.Sleep(0);Console.WriteLine("Main thread {0} does some work.", Thread.CurrentThread.ManagedThreadId);//調用EndInvoke檢索結果string returnValue = caller.EndInvoke(out threadId, result);Console.WriteLine("The call executed on thread {0},with return value \"{1}\".", threadId, returnValue);Console.ReadLine();}
3、使用 WaitHandle 等待異步調用
static void Main(string[] args){int threadId;//創建測試類AsyncDemo asyncDemo = new AsyncDemo();//創建委托TestMethodAsyncCaller caller = new TestMethodAsyncCaller(asyncDemo.TestMethod);//初始化異步調用IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);Thread.Sleep(0);Console.WriteLine("Main thread {0} does some work.", Thread.CurrentThread.ManagedThreadId);//sone to do//等待信號result.AsyncWaitHandle.WaitOne();//調用EndInvoke檢索結果string returnValue = caller.EndInvoke(out threadId, result);result.AsyncWaitHandle.Close();Console.WriteLine("The call executed on thread {0},with return value \"{1}\".", threadId, returnValue);Console.ReadLine();}
4、對異步調用的完成情況進行輪詢
static void Main(string[] args){int threadId;//創建測試類AsyncDemo asyncDemo = new AsyncDemo();//創建委托TestMethodAsyncCaller caller = new TestMethodAsyncCaller(asyncDemo.TestMethod);//初始化異步調用IAsyncResult result = caller.BeginInvoke(3000, out threadId, null, null);Thread.Sleep(0);Console.WriteLine("Main thread {0} does some work.", Thread.CurrentThread.ManagedThreadId);// 等待完成.while (result.IsCompleted == false){Thread.Sleep(250);Console.Write(".");}//調用EndInvoke檢索結果string returnValue = caller.EndInvoke(out threadId, result);Console.WriteLine("The call executed on thread {0},with return value \"{1}\".", threadId, returnValue);Console.ReadLine();}
5、異步調用完成時執行回調方法
static void Main(string[] args){int threadId = 0;//創建測試類AsyncDemo asyncDemo = new AsyncDemo();//創建委托TestMethodAsyncCaller caller = new TestMethodAsyncCaller(asyncDemo.TestMethod);//初始化異步調用IAsyncResult result = caller.BeginInvoke(3000, out threadId, new AsyncCallback(callbackMethod), "The call executed on thread {0},with return value \"{1}\".");Console.WriteLine("Main thread {0} does some work.", Thread.CurrentThread.ManagedThreadId);Thread.Sleep(5000);Console.WriteLine("The main thread ends.");Console.ReadLine();}private static void callbackMethod(IAsyncResult ar){//檢索委托.AsyncResult result = (AsyncResult)ar;TestMethodAsyncCaller caller = (TestMethodAsyncCaller)result.AsyncDelegate;//格式字符串作為狀態信息被傳遞string formatString = (string)ar.AsyncState;int threadId = 0;//調用EndInvoke檢查結果.string returnValue = caller.EndInvoke(out threadId, ar);//用格式字符串格式化輸出信息Console.WriteLine(formatString, threadId, returnValue);}