Class Program {static void Main (string[] args) {Console.WriteLine ("I am the main thread, Threads id:{0}", thre Ad. Currentthread.managedthreadid); Testasync (); Console.ReadLine (); } static async void Testasync () {Console.WriteLine ("Before calling Getreturnresult (), Thread id:{0}. Current time: {1} ", Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString (" Yyyy-mm-dd hh:MM:ss ")); var name = Getreturnresult (); Console.WriteLine ("After calling Getreturnresult (), Thread id:{0}. Current time: {1} ", Thread.CurrentThread.ManagedThreadId, DateTime.Now.ToString (" Yyyy-mm-dd hh:MM:ss ")); The asynchronous callback content//In the await and after the code will block Console.WriteLine ("Get the result of the Getreturnresult () method: {0}.") Current time: {1} ", await name, DateTime.Now.ToString (" Yyyy-mm-dd hh:MM:ss ")); Console.WriteLine ("The content after the await. Current time: {0} ", DateTime.Now.ToString (" Yyyy-mm-dd hh:MM:ss ")); Asynchronous callback Content} static async task<string> Getreturnresult () {Console.WriteLine ("Thread id:{0} before executing Task.run", Thread.CurrentThread.ManagedThreadId); return await Task.run (() = {Thread.Sleep (5000); Console.WriteLine ("Getreturnresult () method inside thread ID: {0}", Thread.CurrentThread.ManagedThreadId); Return "I am the returned value"; }); } }
Task,async,await in C # async