Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            //action、func、task
            Task <int> task = new Task <int>(a => { return((int)a + 1); }, 1);

            task.Start();
            Console.WriteLine(task.Result);
            //func
            Task <int> task1 = new Task <int>((a) => { return((int)a + 1); }, 2);

            task1.Start();
            Console.WriteLine(task1.Result);
            //action
            Task task2 = new Task((a) => { Console.WriteLine(a); }, "a");

            task2.Start();
            CancellationTokenSource souce = new CancellationTokenSource();
            CancellationToken       token = souce.Token;
            var t = Do.ExcuteAsync(token);

            Thread.Sleep(3000);   //挂起 3 秒
            // souce.Cancel();    //传达取消请求
            t.Wait(token);
            Console.WriteLine($"{nameof(token.IsCancellationRequested)}:{token.IsCancellationRequested}");
            Console.ReadLine();
        }