none
Task.Factory.StartNew cancellation token parameter

    Question

  • Hi, folks!

    My question is about task cancellation. I have following code:
    CancellationTokenSource cts = new CancellationTokenSource();
    CancellationToken token = cts.Token;
    
    Task t1 = Task.Factory.StartNew(() =>
    {
        while (true)
        {
            if (token.IsCancellationRequested)
            {
                Console.WriteLine("Cancelled");
                break;
            }
            Thread.Sleep(1000);
        }
    });
    And in some other place I call cts.Cancel() - it's working, and I use defined early token value.
    By the way, I can start task as follow:
    Task t1 = Task.Factory.StartNew(() =>
    {
        //...
    }, token);
    And what the role of the token parameter, which I supplied? May be I can access it in the task body? If so, how can I do it?

    Thanks in advance!
    Wednesday, February 17, 2010 9:11 AM

Answers

  • 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).

    Wednesday, February 17, 2010 5:51 PM

All replies

  • I found the answer in the MSDN library:
    http://msdn.microsoft.com/en-us/library/dd997396(VS.100).aspx

    See: ThrowIfCancellationRequested -> Canceled or Faulted state.
    Wednesday, February 17, 2010 1:22 PM
  • Thank you for reply.

    I read this MSDN article too. In the code snippet, ThrowIfCancellationRequested called on token object, which are available in the delegate body. And it's not depend on which overload of StartNew() is called - with token param, or without.

    I try to reformulate my question. In this piece of code:
    CancellationTokenSource cts = new CancellationTokenSource();
    CancellationToken token = cts.Token;
    
    Task t1 = Task.Factory.StartNew(() =>DoWork(), token);
    
    //...
    
    private void DoWork()
    {
       while (true)
        {
            //cancellation token is not avalable here!
            if (token.IsCancellationRequested)
            {
                Console.WriteLine("Cancelled");
                break;
            }
            Thread.Sleep(1000);
        }
    }
    token is not available, because the body of my DoWork method is don't have any params.
    But I call StartNew with token parameter. Is it any way to use this token object, that I supplied as a param, in the DoWork body?
    If so, how can I do it?
    If no way, I don't understand the purpose of this token param.
    Wednesday, February 17, 2010 2:30 PM
  • 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).

    Wednesday, February 17, 2010 5:51 PM
  • Great! Thank you for explanation, Stephen!
    Wednesday, February 17, 2010 6:39 PM
  • 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).

    Thanks, this explains a lot.
    Tuesday, April 12, 2011 5:05 AM