Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4330158
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T09:55:28+00:00 2026-05-21T09:55:28+00:00

Guys, I used to use a method like Task.Factory.StartNew(new Action(()=>{}), cancellationToken); I have a

  • 0

Guys, I used to use a method like Task.Factory.StartNew(new Action(()=>{}), cancellationToken);

I have a question about the second argument cancellationToken. Because cancellationToken is a variable in my method, in the lambda expression, I can cancel my task using the field just like the msdn does; I’m not sure it’s recommend. In this case, is the second argument necessary here? It is passed in the StartNew method, but not actually used. Is there a scenario we need use this argument?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T09:55:29+00:00Added an answer on May 21, 2026 at 9:55 am

    You need this argument if you want to cancel the Task. If you’re application doesn’t support or require cancellation then you could just say

    Task.Factory.StartNew(() => { ... });
    

    Note that cancellation is cooperative so your code must poll for cancellation and respond accordingly.

    For example:

    CancellationTokenSource cts = new CancellationTokenSource();
    CancellationToken token = cts.Token;
    
    Task myTask = Task.Factory.StartNew(() =>
        {
            for (...)
            {
                token.ThrowIfCancellationRequested();
    
                // Body of for loop.
            }
        }, token);
    
        // ... elsewhere ...
        cts.Cancel();
    

    You have to pass the cancellation token to the method otherwise it’s not attached to the task. The code within method is only using the token to respond to cancelation, either throwing or using the IsCancellationRequested to shut down. Although a task could cancel itself from within the lambda I guess. The task itself needs the token too.

    here’s some further clarification:

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

    From http://social.msdn.microsoft.com/Forums/en/parallelextensions/thread/c2f614f6-c96c-4821-84cc-050b21aaee45

    See general discussion of cancellation here:

    http://msdn.microsoft.com/en-us/library/ff963549.aspx for further discussion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked a question yesterday about password safety... I am new at security... I
I have a Question for you guys.I have 2 unit tests which are calling
Hai guys, How to include inline jquery scripts used in an aspx to a
Guys I have an activity which has a home button, when the button is
guys, I would like to you evaluate next code below. As you see, I
guys. I have a strange problem. I try to write unit-tests to web-app. I
I would like write a script to automate a certain task on windows. I
I have one WCF which i have used in my project. Now i want
Hey guys i want to use an php variable as a part of a
So I'm finally starting learn efficient programming (Used to use one class for everything),

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.