What is the use of passing CancellationToken to Task Class constructor?

走远了吗. 提交于 2019-12-03 15:04:22

问题


Here is a sample code that creates a new task that simulates a long running process.There is nothing much on the task as such and purely focuses on the cancelling features.I am using cancellation token to cancel the task and the code works fine for me.

       CancellationTokenSource CTS= new CancellationTokenSource();

       Task<Boolean> PTask = new Task<Boolean>(() => 
       {
           while (true)
           {
               if (!CTS.Token.IsCancellationRequested)
               {
                  Thread.Sleep(5000);
               }
               else{Console.WriteLine("Thread Cancelled");break;}
           }
           return true;

       }, CTS.Token, TaskCreationOptions.None);

       PTask.Start();
       Console.WriteLine("Hit Enter to cancel the Secondary thread you have started");
       Console.ReadLine();
       CTS.Cancel();
       System.Console.WriteLine(PTask.Result);

    }
}

But onething that i could not understand is the token parameter(CTS.Token) that is being passed on to the Task Constructor.What is the actual use of passing the parameter,when i can actually cancel the task even without passing token to the constructor.

Down below is a slightly modified version that works without the token parameter.

      CancellationTokenSource CTS= new CancellationTokenSource();
      Task<Boolean> PTask = new Task<Boolean>(() => 
       {
           while (true)
           {
               if (!CTS.Token.IsCancellationRequested)
               {
                   Thread.Sleep(5000);
               }
               else
               {
                   Console.WriteLine("Thread Cancelled");
                   break;
               }
       };

回答1:


UPDATE: The following msdn question describes the reason:

Passing a token into StartNew associates the token with the Task. This has two primary benefits:

  1. If the token has cancellation requested prior to the Task starting to execute, the Task won't execute. Rather than transitioning to Running, it'll immediately transition to Canceled. This avoids the costs of running the task if it would just be canceled while running anyway.

  2. If the body of the task is also monitoring the cancellation token and throws an OperationCanceledException containing that token (which is what ThrowIfCancellationRequested does), then when the task sees that OCE, it checks whether the OCE's token matches the Task's token. If it does, that exception is viewed as an acknowledgement of cooperative cancellation and the Task transitions to the Canceled state (rather than the Faulted state).



来源:https://stackoverflow.com/questions/10848484/what-is-the-use-of-passing-cancellationtoken-to-task-class-constructor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!