namespace AsyncDemo { internal class Program { static void Log(string msg) { Console.WriteLine($"{DateTime.Now.ToString("HH:mm:ss.fff")} {msg}"); } static async Task Worker(string name, int time = 1000) { await Task.Delay(time); Log(name); } static void WorkerSync() { Worker("同步").Wait(); // 等待异步方法完成->同步 } static async void WorkerASync() { // 方法内是同步 await Worker("异步等待::A"); await Worker("异步等待::B"); } static void Main(string[] args) { WorkerSync();// 同步方法,等待内部执行完,再执行下面代码 WorkerASync();// 异步方法,跳出内部执行,执行下面代码 Log("End"); Console.ReadKey(); // 输出结果: //11:58:25.245 同步 //11:58:25.260 End //11:58:26.265 异步等待::A //11:58:27.276 异步等待::B } } }
标签:异步,同步,58,C#,void,Worker,static From: https://www.cnblogs.com/chen1880/p/17681989.html