前正无生意,且记Task.ContinueWith之用法。

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("主线程开始");
            Task<string> task = Task<string>.Run(() => {
                Thread.Sleep(2000);
                return Thread.CurrentThread.ManagedThreadId.ToString();
            });

            task.GetAwaiter().OnCompleted(() =>
            {
                Console.WriteLine(task.Result);
            });
            //ContonieWith会在task上一个任务执行完毕之后,继续执行新的任务
            task.ContinueWith(m => { Console.WriteLine("第一个任务结束啦!我是第二个任务"); });
            Console.WriteLine("主线程结束");
            Console.Read();
        }
    }
}

 

相关文章:

  • 2022-02-06
  • 2021-10-19
  • 2022-01-17
  • 2022-12-23
  • 2021-08-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-12-09
  • 2022-12-23
  • 2021-08-12
  • 2021-10-30
  • 2022-12-23
相关资源
相似解决方案