码迷,mamicode.com
首页 > 其他好文 > 详细

使用Async和Await的异步编程

时间:2020-07-01 13:03:16      阅读:47      评论:0      收藏:0      [点我收藏+]

标签:inf   ret   ide   tps   线程   异步   官方   class   guid   

官方文档

技术图片

遇到真正的await后,由新线程接管,即操作开始异步。

技术图片

不阻塞主线程

技术图片

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
            Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}:{SayAsync().Status}");
            Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
            Console.ReadKey(true);
        }

        public static async Task<string> SayAsync()
        {
            Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}_SayAsync");

            var s = await Task<string>.Run(async () =>
            {
                await Task.Delay(TimeSpan.FromSeconds(2));
                Console.WriteLine("---------------两秒后----------------");
                Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
                return "vvull";
            });
            Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}:{s}");
            Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
            return s;
        }

    }

阻塞主线程

技术图片

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
            Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}:{SayAsync().Result}");//阻塞主线程
            Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}");
            Console.ReadKey(true);
        }

        public static async Task<string> SayAsync()
        {
            Console.WriteLine($"主线程{Thread.CurrentThread.ManagedThreadId}_SayAsync");

            var s = await Task<string>.Run(async () =>
            {
                await Task.Delay(TimeSpan.FromSeconds(2));
                Console.WriteLine("---------------两秒后----------------");
                Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
                return "vvull";
            });
            Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}:{s}");
            Console.WriteLine($"子线程{Thread.CurrentThread.ManagedThreadId}");
            return s;
        }

    }

进阶版

技术图片

class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->主线程");
            giaoAsync();
            Thread.Sleep(3000);
            Console.WriteLine("---------------三秒后----------------");
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->主线程");
            Console.ReadKey();
        }
        static async Task giaoAsync()
        {
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao");

            //Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao:{await SayAsync()}");//本句以后由新线程接管
            //Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao*");

            var s = await SayAsync();//由新线接管
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao**_{s}");
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao**");

            var ss = await SayAsync();//由新线接管
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao***_{ss}");
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->giao***");
        }

        public static async Task<string> SayAsync()
        {
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync");

            var s = await Task<string>.Run(async () =>
            {                
                Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync*");
                await Task.Delay(TimeSpan.FromSeconds(0.001));
                Console.WriteLine("---------------两秒后----------------");
                Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync**");
                return "vvull";
            });
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync**_{s}");
            Console.WriteLine($"{Thread.CurrentThread.ManagedThreadId}->SayAsync**");
            return s;
        }

    }

使用Async和Await的异步编程

标签:inf   ret   ide   tps   线程   异步   官方   class   guid   

原文地址:https://www.cnblogs.com/vvull/p/13218369.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!