e媒网络

一切皆可能 e媒网络 http://www.eMay.net

博客园 首页 新随笔 联系 订阅 管理
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}");
        }
    }
}

 

posted on 2023-06-17 23:07  e媒网络技术团队  阅读(19)  评论(0编辑  收藏  举报