原始碼
https://gitee.com/s0611163/AsyncAwaitDemo
為什麼會研究這個?
我們專案的客戶端和服務端通訊用的是WCF,我就想,能不能用非同步的方式呼叫WCF服務呢?或者說能不能用async await的方式呼叫WCF服務呢?
然後我發現WCF是通過BeginXXX和EndXXX這種回撥的方式實現非同步的,似乎不支援async await語法糖,那隻能通過狀態機的方式實現了,不然多個請求就會寫成回撥地獄。
如果用Task.Run包起來,裡面再寫個Wait,這就是假非同步,還不如不用非同步,按原來非非同步的方式寫。
研究了回撥和狀態機之後,又看了相關的部落格,就像一篇部落格裡說的,呼叫WCF不難,非同步呼叫WCF不難,非同步呼叫WCF並保證呼叫順序也不難,難的是實現了這些之後,程式碼的可讀性和可維護性。
所以我的結論是,呼叫WCF,如果沒有特殊需求,還是不要非同步的好,寫起來複雜。
C# 同步 非同步 回撥 狀態機 async await Demo
主要演示了不使用async、await語法糖,通過回撥函式和狀態機實現非同步
為什麼要寫這個Demo?
為了幫助理解非同步,async、await經過編譯生成的狀態機程式碼,有點複雜,這個Demo裡寫了一個相對簡單的狀態機程式碼,便於理解
程式碼說明
程式碼中主要寫了三種下載檔案的示例
- 同步方式下載檔案,為了防止下載較大檔案時卡住介面,程式碼線上程中執行,檔案下載完成之前,始終佔用一個執行緒
- 非同步方式下載檔案,使用了async、await語法糖,下載檔案時,可以看到,workerThreads(可用執行緒數)和completionPortThreads(可用非同步執行緒數)會發生變化,但是不會長時間佔用一個執行緒
- 非同步方式下載檔案,不使用async、await語法糖,通過回撥函式和狀態機實現,workerThreads(可用執行緒數)和completionPortThreads(可用非同步執行緒數)會發生變化,但是不會長時間佔用一個執行緒
結論:非同步的本質是回撥,非同步是回撥的語法糖
- 相比同步方式,使用非同步方式下載檔案時,忽略掉誤差,下載速度並沒有更快,非同步的主要優點是不會長時間佔用一個執行緒
- 在沒有async、await語法糖時,使用回撥函式和狀態機也可以實現非同步,但程式碼寫的不夠優雅,心智負擔重,所以async、await的另一個優點是使程式碼簡單
- 非同步的本質就是回撥,C#非同步底層是通過系統級回撥和狀態機實現的,async、await會被編譯成狀態機程式碼,相關於用程式碼生成器生成了程式碼,但這個程式碼自己寫的話,心智負擔重
- 不使用async、await語法糖的前提下,使用狀態機可以避免回撥地獄
- 即使不使用async、await語法糖,依然感受到了非同步的侵入性,沒辦法在底層完全封裝起來,程式碼一直侵入到控制元件的事件裡,使用async、await也是,你從程式碼的底層,寫async一直寫到控制元件的事件層
思考:
經測試,button4_Click、button5_Click、button6_Click在下載大檔案時,耗時較長,但是介面並不會卡住,依然可以自由拖動窗體或點選其它按鈕
button4_Click、button5_Click、button6_Click方法分別呼叫了AsyncFunctionWithCallback方法
AsyncFunctionWithCallback方法呼叫了HttpUtil類的HttpDownloadFileAsyncWithCallback方法
請仔細觀察上面的方法,它們沒有使用new Thread、Task.Run、Task.Factory.StartNew等執行緒相關的方法,
也就是說執行長時間操作,沒有阻塞介面,但是又沒有使用執行緒,
實際上下載檔案過程中,沒有使用C#的CLR執行緒池,測試時,workerThreads數量不會變化,但是你會看到completionPortThreads數量變化,
下載檔案的過程使用了非同步執行緒池,但是下載大檔案時,並不會長時間佔用非同步執行緒池
Demo截圖
測試程式碼Form1.cs
using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using Utils;
namespace AsyncAwaitDemo
{
public partial class Form1 : Form
{
private DateTime _dt1;
private System.Timers.Timer _timer;
private string _testFileDownloadUrl = "http://desk-fd.zol-img.com.cn/t_s1920x1080c5/g6/M00/06/00/ChMkKWHXqV-IJFpUAEJatCdq_LUAAXW8AA1PvwAQlrM029.jpg?downfile=1642227460763.jpg"; //檔案下載測試URL
//private string _testFileDownloadUrl = "https://down21.xiazaidb.com/app/xiaomanghe.apk"; //檔案下載測試URL
//private string _testFileDownloadUrl = "https://codeload.github.com/0611163/DBHelper/zip/refs/heads/master"; //檔案下載測試URL
//private string _testFileDownloadUrl = "http://down-ww5.537a.com/soft/3/ce/com.yhong.muchun_51982ada.apk"; //檔案下載測試URL
//private string _testFileDownloadUrl = "https://dl.360safe.com/netunion/20140425/360se+191727+n3f07b78190.exe"; //檔案下載測試URL 大一點的檔案
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//定時器會使用執行緒池中的一個執行緒
_timer = new System.Timers.Timer();
_timer.Interval = 100;
_timer.Elapsed += _timer_Elapsed;
_timer.Start();
}
#region Log
private void Log(string log)
{
if (!this.IsDisposed)
{
string msg = DateTime.Now.ToString("mm:ss.fff") + " " + log + "\r\n\r\n";
if (this.InvokeRequired)
{
this.BeginInvoke(new Action(() =>
{
textBox1.AppendText(msg);
}));
}
else
{
textBox1.AppendText(msg);
}
}
}
#endregion
#region _timer_Elapsed
private void _timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
int workerThreads;
int completionPortThreads;
int maxWorkerThreads;
int maxCompletionPortThreads;
ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
ThreadPool.GetMaxThreads(out maxWorkerThreads, out maxCompletionPortThreads);
this.BeginInvoke(new Action(() =>
{
label1.Text = "程式當前使用執行緒數:" + (maxWorkerThreads - workerThreads) + " workerThreads:" + workerThreads.ToString() + " completionPortThreads:" + completionPortThreads;
}));
}
#endregion
#region button1_Click 測試同步方法
private void button1_Click(object sender, EventArgs e)
{
Task.Run(() => //下載檔案是耗時操作,需要線上程中執行,否則介面卡住
{
Log("開始");
DateTime dt = DateTime.Now;
//輸入引數
string arg1 = "1";
string arg2 = "2";
string arg3 = "3";
//方法呼叫
string result1 = SyncFunction(arg1);
string result2 = SyncFunction(arg2);
string result3 = SyncFunction(arg3);
//輸出結果
Log(result1);
Log(result2);
Log(result3);
Log("結束,耗時:" + DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000"));
});
}
#endregion
#region button2_Click 測試非同步方法(三個非同步方法並行,按順序輸出)
private async void button2_Click(object sender, EventArgs e)
{
Log("開始");
DateTime dt = DateTime.Now;
//輸入引數
string arg1 = "1";
string arg2 = "2";
string arg3 = "3";
//方法呼叫
var t1 = AsyncFunction(arg1);
var t2 = AsyncFunction(arg2);
var t3 = AsyncFunction(arg3);
string result1 = await t1;
string result2 = await t2;
string result3 = await t3;
//輸出結果
Log(result1);
Log(result2);
Log(result3);
Log("結束,耗時:" + DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000"));
}
#endregion
#region button3_Click 測試非同步方法(三個非同步方法順序執行,按順序輸出)
private async void button3_Click(object sender, EventArgs e)
{
Log("開始");
DateTime dt = DateTime.Now;
//輸入引數
string arg1 = "1";
string arg2 = "2";
string arg3 = "3";
//方法呼叫
string result1 = await AsyncFunction(arg1);
string result2 = await AsyncFunction(arg2);
string result3 = await AsyncFunction(arg3);
//輸出結果
Log(result1);
Log(result2);
Log(result3);
Log("結束,耗時:" + DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000"));
}
#endregion
#region button4_Click 測試狀態機(三個非同步方法順序執行,按順序輸出)(等價於button3_Click)
private void button4_Click(object sender, EventArgs e)
{
List<object> resultList = new List<object>();
//回撥、狀態機,不使用async await語法糖
Action<int, object> awaitAction = null;
awaitAction = (state, result) =>
{
//輸入引數
string arg1 = "1";
string arg2 = "2";
string arg3 = "3";
//方法呼叫
if (state == 0)
{
Log("開始");
_dt1 = DateTime.Now;
AsyncFunctionWithCallback(arg1, r => awaitAction(1, r));
return;
}
if (state == 1)
{
resultList.Add(result);
AsyncFunctionWithCallback(arg2, r => awaitAction(2, r));
return;
}
if (state == 2)
{
resultList.Add(result);
AsyncFunctionWithCallback(arg3, r => awaitAction(3, r));
return;
}
//輸出結果
if (state == 3)
{
resultList.Add(result);
foreach (object item in resultList)
{
Log(item != null ? item.ToString() : "null");
}
Log("結束,耗時:" + DateTime.Now.Subtract(_dt1).TotalSeconds.ToString("0.000"));
return;
}
};
awaitAction(0, null);
}
#endregion
#region button5_Click 測試狀態機(三個非同步方法並行,按順序輸出)(等價於button2_Click)
private void button5_Click(object sender, EventArgs e)
{
object[] resultArray = new object[3];
//是否已呼叫回撥函式
bool _completedCalled = false;
//鎖
object _lock = new object();
//輸出結果
Action<object[]> output = arr =>
{
if (arr.Count(a => a != null) == 3)
{
lock (_lock)
{
if (!_completedCalled)
{
_completedCalled = true;
foreach (object item in arr)
{
Log(item != null ? item.ToString() : "null");
}
Log("結束,耗時:" + DateTime.Now.Subtract(_dt1).TotalSeconds.ToString("0.000"));
}
}
}
};
//回撥、狀態機,不使用async await語法糖
Action<int, object> awaitAction = null;
awaitAction = (state, result) =>
{
//輸入引數
string arg1 = "1";
string arg2 = "2";
string arg3 = "3";
//方法呼叫
if (state == 0)
{
Log("開始");
_dt1 = DateTime.Now;
AsyncFunctionWithCallback(arg1, r => awaitAction(1, r));
AsyncFunctionWithCallback(arg2, r => awaitAction(2, r));
AsyncFunctionWithCallback(arg3, r => awaitAction(3, r));
return;
}
//輸出結果
if (state == 1)
{
resultArray[0] = result;
output(resultArray);
return;
}
if (state == 2)
{
resultArray[1] = result;
output(resultArray);
return;
}
if (state == 3)
{
resultArray[2] = result;
output(resultArray);
return;
}
};
awaitAction(0, null);
}
#endregion
#region button6_Click 測試狀態機(三個非同步方法並行,按順序輸出)(等價於button2_Click)(相對於button5_Click更優雅的實現)
private void button6_Click(object sender, EventArgs e)
{
Log("開始");
DateTime dt = DateTime.Now;
//輸入引數
string arg1 = "1";
string arg2 = "2";
string arg3 = "3";
//操作結果
object result1 = null;
object result2 = null;
object result3 = null;
//方法呼叫
Await await = new Await();
await.Add(() => AsyncFunctionWithCallback(arg1, r => await.Collect(r, out result1)))
.Add(() => AsyncFunctionWithCallback(arg2, r => await.Collect(r, out result2)))
.Add(() => AsyncFunctionWithCallback(arg3, r => await.Collect(r, out result3)))
.Completed(resultList => //輸出結果
{
Log(result1 != null ? result1.ToString() : "null");
Log(result2 != null ? result2.ToString() : "null");
Log(result3 != null ? result3.ToString() : "null");
Log("結束,耗時:" + DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000"));
})
.Start();
}
#endregion
#region button7_Click 不使用狀態機,執行帶回撥的非同步方法(基本等價於button1_Click)
private void button7_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
Log("開始");
DateTime dt = DateTime.Now;
//輸入引數
string arg1 = "1";
string arg2 = "2";
string arg3 = "3";
//操作結果
object result1 = null;
object result2 = null;
object result3 = null;
//方法呼叫
AsyncFunctionWithCallback(arg1, r => result1 = r);
AsyncFunctionWithCallback(arg2, r => result2 = r);
AsyncFunctionWithCallback(arg3, r => result3 = r);
//等待三個操作結果全部返回,三個操作結果返回之前,始終佔用一個執行緒
while (result1 == null || result2 == null || result3 == null)
{
Thread.Sleep(1);
}
//輸出結果
Log(result1 != null ? result1.ToString() : "null");
Log(result2 != null ? result2.ToString() : "null");
Log(result3 != null ? result3.ToString() : "null");
Log("結束,耗時:" + DateTime.Now.Subtract(dt).TotalSeconds.ToString("0.000"));
});
}
#endregion
#region 同步方法
/// <summary>
/// 同步方法
/// </summary>
private string SyncFunction(string arg)
{
MemoryStream ms = HttpUtil.HttpDownloadFile(_testFileDownloadUrl);
return "同步方法 arg=" + arg + " 下載檔案的位元組陣列長度=" + ms.Length;
}
#endregion
#region 非同步方法
/// <summary>
/// 非同步方法
/// </summary>
private async Task<string> AsyncFunction(string arg)
{
MemoryStream ms = await HttpUtil.HttpDownloadFileAsync(_testFileDownloadUrl);
return "非同步方法 arg=" + arg + " 下載檔案的位元組陣列長度=" + ms.Length;
}
#endregion
#region 帶回撥的非同步方法
/// <summary>
/// 帶回撥的非同步方法
/// </summary>
/// <param name="arg">傳入引數</param>
/// <param name="callback">回撥</param>
/// <param name="nextState">下一個狀態</param>
private void AsyncFunctionWithCallback(string arg, Action<object> callback)
{
HttpUtil.HttpDownloadFileAsyncWithCallback(_testFileDownloadUrl, new HttpUtilAsyncState() { State = 0 }, obj =>
{
if (obj is Exception)
{
Exception ex = obj as Exception;
Log("錯誤:" + ex.Message);
}
else
{
MemoryStream ms = obj as MemoryStream;
string result = "帶回撥的非同步方法 arg=" + arg + " 下載檔案的位元組陣列長度=" + ms.Length;
callback(result);
}
});
}
#endregion
#region 用Task模擬的非同步方法
/*
#region 非同步方法
/// <summary>
/// 非同步方法
/// </summary>
private Task<string> AsyncFunction(string arg)
{
return Task.Run<string>(() =>
{
Thread.Sleep(2000);
return "非同步方法 arg=" + arg;
});
}
#endregion
#region 帶回撥的非同步方法
/// <summary>
/// 帶回撥的非同步方法
/// </summary>
/// <param name="arg">傳入引數</param>
/// <param name="callback">回撥</param>
/// <param name="nextState">下一個狀態</param>
private void AsyncFunctionWithCallback(string arg, Action<object> callback)
{
Task.Run(() =>
{
if (arg == "1")
{
Thread.Sleep(2000);
}
else
{
Thread.Sleep(2000);
}
string result = "帶回撥的非同步方法 arg=" + arg;
callback(result);
});
}
#endregion
*/
#endregion
}
}
非同步工具類Await.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Utils
{
/// <summary>
/// 非同步工具類
/// 代替async await語法糖
/// </summary>
public class Await
{
/// <summary>
/// 狀態
/// </summary>
private int _state { get; set; }
/// <summary>
/// 非同步操作集合
/// </summary>
private List<Action> _actionList = new List<Action>();
/// <summary>
/// 操作結果集合
/// </summary>
private List<object> _resultList = new List<object>();
/// <summary>
/// 完成後的回撥
/// </summary>
private Action<List<object>> _completedCallback;
/// <summary>
/// 是否已呼叫回撥函式
/// </summary>
private bool _completedCalled = false;
/// <summary>
/// 鎖
/// </summary>
private object _lock = new object();
/// <summary>
/// 非同步工具類 建構函式
/// </summary>
public Await()
{
_state = 0;
}
/// <summary>
/// 新增一個非同步操作
/// </summary>
public Await Add(Action action)
{
_actionList.Add(action);
return this;
}
/// <summary>
/// 開始執行
/// </summary>
public Await Start()
{
foreach (Action action in _actionList)
{
action();
}
return this;
}
/// <summary>
/// 收集結果
/// </summary>
public Await Collect(object result, out object outResult)
{
outResult = result;
_resultList.Add(result);
lock (_lock)
{
_state++;
if (_state == _actionList.Count && _completedCallback != null && !_completedCalled)
{
_completedCalled = true;
_completedCallback(_resultList);
}
}
return this;
}
/// <summary>
/// 註冊完成後的回撥函式
/// </summary>
public Await Completed(Action<List<object>> completedCallback)
{
this._completedCallback = completedCallback;
return this;
}
}
}
檔案下載工具類HttpUtil.cs
下面的程式碼使用了三種方式實現檔案下載
- 同步方式實現檔案下載
- 非同步方式實現檔案下載
- 通過回撥和狀態機實現的非同步下載
using Models;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Utils
{
/// <summary>
/// Http上傳下載檔案
/// </summary>
public class HttpUtil
{
#region HttpDownloadFile 下載檔案
/// <summary>
/// 下載檔案
/// </summary>
/// <param name="url">下載檔案url路徑</param>
/// <param name="cookie">cookie</param>
public static MemoryStream HttpDownloadFile(string url, CookieContainer cookie = null, WebHeaderCollection headers = null)
{
try
{
// 設定引數
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.CookieContainer = cookie;
if (headers != null)
{
foreach (string key in headers.Keys)
{
request.Headers.Add(key, headers[key]);
}
}
//傳送請求並獲取相應回應資料
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程式才開始向目標網頁傳送Post請求
Stream responseStream = response.GetResponseStream();
//建立寫入流
MemoryStream stream = new MemoryStream();
byte[] bArr = new byte[10240];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
stream.Seek(0, SeekOrigin.Begin);
responseStream.Close();
return stream;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region HttpDownloadFile 下載檔案(非同步)
/// <summary>
/// 下載檔案
/// </summary>
/// <param name="url">下載檔案url路徑</param>
/// <param name="cookie">cookie</param>
public static async Task<MemoryStream> HttpDownloadFileAsync(string url, CookieContainer cookie = null, WebHeaderCollection headers = null)
{
try
{
// 設定引數
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.CookieContainer = cookie;
if (headers != null)
{
foreach (string key in headers.Keys)
{
request.Headers.Add(key, headers[key]);
}
}
//傳送請求並獲取相應回應資料
HttpWebResponse response = await request.GetResponseAsync() as HttpWebResponse;
//直到request.GetResponse()程式才開始向目標網頁傳送Post請求
Stream responseStream = response.GetResponseStream();
//建立寫入流
MemoryStream stream = new MemoryStream();
byte[] bArr = new byte[10240];
int size = await responseStream.ReadAsync(bArr, 0, (int)bArr.Length);
while (size > 0)
{
stream.Write(bArr, 0, size);
size = await responseStream.ReadAsync(bArr, 0, (int)bArr.Length);
}
stream.Seek(0, SeekOrigin.Begin);
responseStream.Close();
return stream;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region HttpDownloadFile 下載檔案(基於回撥的非同步)
/// <summary>
/// 下載檔案
/// </summary>
/// <param name="url">下載檔案url路徑</param>
/// <param name="cookie">cookie</param>
public static void HttpDownloadFileAsyncWithCallback(string url, HttpUtilAsyncState state = null, Action<object> callback = null, CookieContainer cookie = null, WebHeaderCollection headers = null)
{
try
{
if (state.State == 0)
{
// 設定引數
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.Method = "GET";
request.CookieContainer = cookie;
if (headers != null)
{
foreach (string key in headers.Keys)
{
request.Headers.Add(key, headers[key]);
}
}
//傳送請求並獲取相應回應資料
request.BeginGetResponse(asyncResult =>
{
HttpUtilAsyncState asyncState = asyncResult.AsyncState as HttpUtilAsyncState;
try
{
HttpWebResponse response = request.EndGetResponse(asyncResult) as HttpWebResponse;
asyncState.ResponseStream = response.GetResponseStream();
HttpDownloadFileAsyncWithCallback(null, asyncState, callback);
}
catch (Exception ex)
{
asyncState.Exception = ex;
asyncState.State = 2;
HttpDownloadFileAsyncWithCallback(null, asyncState, callback);
}
}, new HttpUtilAsyncState { Request = request, State = 1 });
return;
}
if (state.State == 1)
{
byte[] bArr = new byte[10240];
state.ResponseStream.BeginRead(bArr, 0, (int)bArr.Length, asyncResult =>
{
HttpUtilAsyncState asyncState = asyncResult.AsyncState as HttpUtilAsyncState;
try
{
int size = state.ResponseStream.EndRead(asyncResult);
if (size > 0)
{
state.Stream.Write(bArr, 0, size);
HttpDownloadFileAsyncWithCallback(null, asyncState, callback);
}
else
{
asyncState.State = 2;
HttpDownloadFileAsyncWithCallback(null, asyncState, callback);
}
}
catch (Exception ex)
{
asyncState.Exception = ex;
asyncState.State = 2;
HttpDownloadFileAsyncWithCallback(null, asyncState, callback);
}
}, state);
return;
}
if (state.State == 2)
{
if (state.Exception != null)
{
callback(state.Exception);
return;
}
else
{
state.Stream.Seek(0, SeekOrigin.Begin);
state.ResponseStream.Close();
callback(state.Stream);
return;
}
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}