C#??????У????????async??await
???????????? ???????[ 2016/3/7 11:32:38 ] ????????.NET ???????????
????2????? async & await??
static void Main(string[] args)
{
Console.WriteLine("?????????????ID??{0}"?? Thread.CurrentThread.ManagedThreadId);
TestAsync();
Console.ReadLine();
}
static async Task TestAsync()
{
Console.WriteLine("????GetReturnResult()???????ID??{0}????????{1}"?? Thread.CurrentThread.ManagedThreadId?? DateTime.Now.ToString("yyyy-MM-dd hh:MM:ss"));
var name = GetReturnResult();
Console.WriteLine("????GetReturnResult()??????ID??{0}????????{1}"?? Thread.CurrentThread.ManagedThreadId?? DateTime.Now.ToString("yyyy-MM-dd hh:MM:ss"));
Console.WriteLine("???GetReturnResult()??????????{0}????????{1}"?? await name?? DateTime.Now.ToString("yyyy-MM-dd hh:MM:ss"));
}
static async Task<string> GetReturnResult()
{
Console.WriteLine("???Task.Run???? ???ID??{0}"?? Thread.CurrentThread.ManagedThreadId);
return await Task.Run(() =>
{
Thread.Sleep(3000);
Console.WriteLine("GetReturnResult()???????????ID: {0}"?? Thread.CurrentThread.ManagedThreadId);
return "????????";
});
}
?????????
?????????????????????й????
???????????????????????????
??????1????async?????????????棬??????await????????????????????????????????????????
??????2????async?????????????棬??await????????????????????????????????await???????????????????????
??????3??await?????????????????????????????????????async?????????????????Task??????????????????????????await???????????????async??????????????async????????????????Task??
??????4?????async??????????????Task??????????????????????
???????????????????????void??Task????Task<T>?????????async????void??????Task??????
????3??????await??????Task????????????????????н????
????static async Task TestAsync()
????{
????Console.WriteLine("????GetReturnResult()???????ID??{0}????????{1}"?? Thread.CurrentThread.ManagedThreadId?? DateTime.Now.ToString("yyyy-MM-dd hh:MM:ss"));
????var name = GetReturnResult();
????Console.WriteLine("????GetReturnResult()??????ID??{0}????????{1}"?? Thread.CurrentThread.ManagedThreadId?? DateTime.Now.ToString("yyyy-MM-dd hh:MM:ss"));
????Console.WriteLine("???GetReturnResult()??????????{0}????????{1}"?? name.GetAwaiter().GetResult()?? DateTime.Now.ToString("yyyy-MM-dd hh:MM:ss"));
????}
???????????????????????
????name.GetAwaiter()?????????????????TaskAwaiter????????????????????????????????????????????????????????await????????????????????Щ????????????????TaskAwaiter???????
????public struct TaskAwaiter<TResult> : ICriticalNotifyCompletion?? INotifyCompletion
????{
????public bool IsCompleted { get; }
????public TResult GetResult();
????public void OnCompleted(Action continuation);
????public void UnsafeOnCompleted(Action continuation);
????}
????IsCompleted?????????????????????????????ɡ?
????GetResult()???????е??????????????await?????Ч???????
????OnCompleted()???????????У??????????????????С?
????UnsafeOnCompleted()???????? awaiter ????????????????????
???????????????await?????????????????TaskAwaiter?????GetResult()??????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11