解析使用enumerator模式簡化異步操作的詳解
先看一段同步代碼:
public int SumPageSizes(IList<Uri> uris) {
int total = 0;
foreach (var uri in uris) {
statusText.Text = string.Format("Found {0} bytes ...", total);
var data = new WebClient().DownloadData(uri);
total += data.Length;
}
statusText.Text = string.Format("Found {0} bytes total", total);
return total;
}
這段代碼比較簡單,使用同步方式一個一個的獲取Uri的Data,然后進行統(tǒng)計。
如果要使用異步方式一個一個的統(tǒng)計,那應該如何計算呢?
我以前演示過一段丑陋的代碼大致如下:
WebClient webClient = new WebClient();
webClient.DownloadDataCompleted += (s, e) =>
{
// 使用A對象,做些事情。
WebClient webClient2 = new WebClient();
webClient2.DownloadDataCompleted += (s2, e2) =>
{
//使用B對象,做些事情。
// 遞歸的去 DownloadDataAsync。
};
webClient2.DownloadDataAsync(new Uri("B 的地址"));
};
webClient.DownloadDataAsync(new Uri("A 的地址"));
當然如果你確定只有兩個地址的話,這種方法未嘗不可。如果有多個地址的話,則必須遞歸的調用了。
如何使用Enumerator來簡化異步操作:
public void SumPageSizesAsync(IList<Uri> uris) {
SumPageSizesAsyncHelper(uris.GetEnumerator(), 0);
}
private void SumPageSizesAsyncHelper(IEnumerator<Uri> enumerator, int total) {
if (enumerator.MoveNext()) {
statusText.Text = string.Format("Found {0} bytes ...", total);
var client = new WebClient();
client.DownloadDataCompleted += (sender, e) => {
SumPageSizesAsyncHelper(enumerator, total + e.Result.Length);
};
client.DownloadDataAsync(enumerator.Current);
}
else {
statusText.Text = string.Format("Found {0} bytes total", total);
enumerator.Dispose();
}
}
通過SumPageSizesAsyncHelper ,可以實現(xiàn)異步調用A->異步調用B->異步調用C..的方式。
首先解釋下為什么可以,假設uris 有A,B,C.
SumPageSizesAsyncHelper(uris.GetEnumerator(), 0);
方法先調用A,因為A后面還有B,所以enumerator.MoveNext()返回True,
接著在DownloadDataCompleted事件結束后,調用B,同樣,因為B后面還有C,
所以enumerator.MoveNext() 繼續(xù)返回True,接著在DownloadDataCompleted事件后調用C。
在調用C結束后,因為C后面沒有了,所以enumerator.MoveNext() 返回False,
也可以認為全部都下載完畢了。所以返回最終的結果。
如果使用async 和await來實現(xiàn)的話,代碼如下:
public async Task<int> SumPageSizesAsync2(IList<Uri> uris)
{
int total = 0;
Char charText = 'A';
foreach (var uri in uris)
{
var data = await new WebClient().DownloadDataTaskAsync(uri);
total += data.Length;
Console.WriteLine("Thread Id: {0}:調用{1}的地址 Found {2} bytes...{3}",
Thread.CurrentThread.ManagedThreadId, charText, total, DateTime.Now);
charText = Convert.ToChar(charText + 1);
}
Console.WriteLine("Thread Id: {0}:全部完成,Found {1} bytes total {2}",
Thread.CurrentThread.ManagedThreadId, total, DateTime.Now);
return total;
}
相關文章
C# 使用WPF 用MediaElement控件實現(xiàn)視頻循環(huán)播放
在WPF里用MediaElement控件,實現(xiàn)一個循環(huán)播放單一視頻的程序,同時可以控制視頻的播放、暫停、停止。這篇文章給大家介紹了C# 使用WPF 用MediaElement控件實現(xiàn)視頻循環(huán)播放,需要的朋友參考下吧2018-04-04WinForm通過操作注冊表實現(xiàn)限制軟件使用次數(shù)的方法
這篇文章主要介紹了WinForm通過操作注冊表實現(xiàn)限制軟件使用次數(shù)的方法,結合實例形式分析了WinForm操作注冊表的原理、步驟與相關實現(xiàn)技巧,需要的朋友可以參考下2017-06-06