前言
在C# 5.0中,新增了async await 2個關鍵字支援非同步編程的操作。在講述這兩個關鍵字之前,我先總結一下.NET中的常見的非同步編程模型。
非同步編程一直是比較複雜的問題,其中要處理多線程之間的資料同步、擷取進度、可取消、擷取結果、不影響主線程操作、多個任務之間互相不影響等,因此需要設計編程模型去處理此類問題。
從.NET 4.5開始,支援的三種非同步編程模式:
基於事件的非同步編程設計模式 (EAP,Event-based Asynchronous Pattern)
非同步編程模型(APE,Asynchronous Programming Model)
基於任務的編程模型(TAP,Task-based Asynchronous Pattern)
目前新版的.NET是偏向於建議使用TAP方式進行非同步編程,WINRT中的非同步作業就只有TAP的身影,async await關鍵字也只是支援TAP的編程模型。
事件架構非同步模式 - EAP
EAP的編程模式的代碼有以下特點:
將有一個或多個名為 “[方法名稱]Async” 的方法。這些方法可能會建立同步版本的鏡像,這些同步版本會在當前線程上執行相同的操作。
該類還可能有一個 “[方法名稱]Completed” 事件,監聽非同步方法呼叫的結果。
它可能會有一個 “[方法名稱]AsyncCancel”(或只是 CancelAsync)方法,用於取消進行中的非同步作業。
下面是一個符合此模式的類聲明樣本 複製代碼 代碼如下:public class AsyncExample
{
// Synchronous methods.
public int Method1(string param);
public void Method2(double param);
// Asynchronous methods.
public void Method1Async(string param);
public void Method1Async(string param, object userState);
public event Method1CompletedEventHandler Method1Completed;
public void Method2Async(double param);
public void Method2Async(double param, object userState);
public event Method2CompletedEventHandler Method2Completed;
public void CancelAsync(object userState);
public bool IsBusy { get; }
// Class implementation not shown.
}
這裡虛構的 AsyncExample 類有兩個方法,都支援同步和非同步呼叫。同步重載的行為類似於方法調用,它們對調用線程執行操作;如果操作很耗時,則調用的返回可能會有明顯的延遲。非同步重載將在另一個線程上啟動操作,然後立即返回,允許在調用線程繼續執行的同時讓操作“在後台”執行。
System.Net.WebClient 本身就有很多EAP的例子,以它的DownloadString為例,WebClient中跟DownloadString相關的方法有:
DownloadString:同步下載字串資源的方法,此方法阻塞當前線程。
DownloadStringAsync:使用EAP非同步編程模式下載字串資源的方法,此方法不會阻塞當前線程。
DownloadStringCompleted:響應非同步下載時完成的事件。
DownloadProgressChanged:響應非同步下載時進度變化。
調用模型樣本如下: 複製代碼 代碼如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace AsyncTest1.EAP
{
public class EAPRunTest1
{
public static void AsyncRun() {
Utility.Log("AsyncRun:start");
//測試網址
string url = "http://sports.163.com/nba/";
using (WebClient webClient = new WebClient()) {
//監控下載進度
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(webClient_DownloadProgressChanged);
//監控完成情況
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
webClient.DownloadStringAsync(new Uri(url));
Utility.Log("AsyncRun:download_start");
}
}
static void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
string log = "AsyncRun:download_completed";
log += "|cancel=" + e.Cancelled.ToString() ;
if (e.Error != null)
{
//出現異常,就記錄異常
log += "|error=" + e.Error.Message;
}
else {
//沒有出現異常,則記錄結果
log += "|result_size=" + Utility.GetStrLen(e.Result);
}
Utility.Log(log);
}
static void webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Utility.Log("AsyncRun:download_progress|percent=" + e.ProgressPercentage.ToString());
}
}
}
運行結果:
2012-12-28 00:39:39:621 AsyncRun:start
2012-12-28 00:39:40:377 AsyncRun:download_start
2012-12-28 00:39:40:903 AsyncRun:download_progress|percent=1
2012-12-28 00:39:40:933 AsyncRun:download_progress|percent=3
2012-12-28 00:39:40:933 AsyncRun:download_progress|percent=5
2012-12-28 00:39:40:934 AsyncRun:download_progress|percent=5
2012-12-28 00:39:40:975 AsyncRun:download_progress|percent=9
2012-12-28 00:39:41:068 AsyncRun:download_progress|percent=21
2012-12-28 00:39:41:131 AsyncRun:download_progress|percent=29
2012-12-28 00:39:41:182 AsyncRun:download_progress|percent=37
2012-12-28 00:39:41:298 AsyncRun:download_progress|percent=50
2012-12-28 00:39:41:354 AsyncRun:download_progress|percent=58
2012-12-28 00:39:41:447 AsyncRun:download_progress|percent=74
2012-12-28 00:39:41:489 AsyncRun:download_progress|percent=82
2012-12-28 00:39:41:582 AsyncRun:download_progress|percent=100
2012-12-28 00:39:41:582 AsyncRun:download_progress|percent=100
2012-12-28 00:39:41:614 AsyncRun:download_completed|cancel=False|result_size=205568