TaskContinuationOptions.ExecuteSynchronously

public static void Main(string[] args)
        {
            //TaskContinuationOptions.ExecuteSynchronously指示task2使用task1线程来执行,不会重新分配线程
            //Only very short-running continuations should be executed synchronously.
            var t =Task.Run(async () => {
                await Task.Delay(2000);
                Console.WriteLine($"task1 thread id={Thread.CurrentThread.ManagedThreadId},Time={DateTime.Now}");
            }).ContinueWith(rst => {
                Console.WriteLine($"task2 thread id={Thread.CurrentThread.ManagedThreadId},Time={DateTime.Now}");

            }, TaskContinuationOptions.ExecuteSynchronously);//
            t.Wait();
            Console.WriteLine("主线程");
            Console.ReadKey();
        }

 

posted on 2022-03-23 10:22  Shine-Zhong  阅读(76)  评论(0编辑  收藏  举报

导航