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

 

相关文章:

  • 2021-04-13
  • 2022-12-23
  • 2021-06-13
  • 2022-01-10
  • 2021-06-13
  • 2021-05-30
  • 2022-12-23
猜你喜欢
  • 2022-02-19
  • 2022-12-23
  • 2022-12-23
  • 2021-06-10
  • 2021-06-22
  • 2022-12-23
  • 2022-02-27
相关资源
相似解决方案