代码改变世界

async await

2017-09-15 17:44  Carl Xing  阅读(154)  评论(0编辑  收藏  举报

async await 成对出现。执行结果为: end  async 

        static void Main(string[] args)
        {

            DoAsync();
            
            Console.WriteLine("end");

            Console.ReadLine();
        }

        private static async Task DoAsync()
        {
            string ss = await ReturnAsync();
            Console.WriteLine(ss);
        }

        private static async Task<string> ReturnAsync()
        {
            return await Task<string>.Run(() =>
            {
                Thread.Sleep(5000);
                return "async";
            });
        }