using System; using System.Threading; using System.Threading.Tasks; namespace AsyncAwaitDemo { class Program { static void Main(string[] args) { Console.WriteLine($"a1,主线程开始,线程Id:{Thread.CurrentThread.ManagedThreadId}"); TestAsync1(); Console.WriteLine($"a2,主线程开始,线程Id:{Thread.CurrentThread.ManagedThreadId}"); Console.ReadKey(); } public static async Task TestAsync1() { Console.WriteLine($"b1,异步方法开始,线程Id:{Thread.CurrentThread.ManagedThreadId}"); await TestAsync2(); Console.WriteLine($"b2,异步方法开始,线程Id:{Thread.CurrentThread.ManagedThreadId}"); } public static async Task TestAsync2() { Console.WriteLine($"c1,异步方法开始,线程Id:{Thread.CurrentThread.ManagedThreadId}"); await Task.Run(()=> { Thread.Sleep(1000); Console.WriteLine($"c2,异步方法执行,线程Id:{Thread.CurrentThread.ManagedThreadId}"); }); Console.WriteLine($"c3,异步方法结束,线程Id:{Thread.CurrentThread.ManagedThreadId}"); } } }
标签:Console,CurrentThread,Thread,ManagedThreadId,Demo,Await,线程,WriteLine,Async From: https://www.cnblogs.com/exesoft/p/17488463.html