????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()??????