前正无生意,且记录Task回调之用法。

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);
            });
            Console.WriteLine(task.Result); //阻塞线程
            Console.WriteLine("主线程结束");
            Console.Read();
        }
    }
}

 

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