前正无生意,且记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();
        }
    }
}

 

posted on 2018-01-23 10:24  chester·chen  阅读(2150)  评论(0编辑  收藏  举报