• CancellationTokenSource 取消任务


    using System;
    using System.Threading;
    using System.Threading.Tasks;
    
    namespace ConsoleApp1
    {
        class Program
        {
            //声明CancellationTokenSource对象
            static CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
    
            //程序入口
            static void Main(string[] args)
            {
                Task.Factory.StartNew(MyTask, cancelTokenSource.Token);
    
                Console.WriteLine("请按回车键(Enter)停止");
                Console.ReadLine();
    
                cancelTokenSource.Cancel();
    
                Console.WriteLine("已停止");
                Console.ReadLine();
            }
    
            //测试方法
            static void MyTask()
            {
                //判断是否取消任务
                while (!cancelTokenSource.IsCancellationRequested)
                {
                    Console.WriteLine(DateTime.Now);
                    Thread.Sleep(1000);
                }
            }
        }
    }
  • 相关阅读:
    1104
    HDU 1575
    hdu 1142(DFS+dijkstra)
    hdu 1015(DFS)
    hdu 1342(DFS)
    hdu 1181(DFS)变 形 课
    hdu 1312(DFS)
    hdu 5976 Detachment
    hdu 5795
    UVa 11729
  • 原文地址:https://www.cnblogs.com/chenyishi/p/10172211.html
Copyright © 2020-2023  润新知