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 7646021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:05:57+00:00 2026-05-31T10:05:57+00:00

Lets say I have something like this: private CancellationTokenSource myToken; public void MyMyMethod() {

  • 0

Lets say I have something like this:

private CancellationTokenSource myToken;

public void MyMyMethod()
{
  myToken = new CancellationTokenSource();
  var task = Task.Factory.StartNew(() => DoIt(myToken.Token), myToken.Token);

  Thread.Sleep(100);
  myToken.Cancel();
}

public void MyOtherMethod()
{
  myToken.Cancel();
}

private void DoIt(CancellationToken token)
{
    token.ThrowIfCancellationRequested();
    try
    {
       for (int i = 0; i < 1000000; i++)
       {
         Console.WriteLine(i);
        }
    }
    catch (Exception ex)
    {
       string s = "";
    }
}

If I call myToken.Cancel will it stop the task in the DoIt method abruptly or do I have to pass in the token to DoIt and call myToken.ThrowIfCancellationRequested() so that when the Cancel is called it will throw the exception and stop abruptly?

Can I not do this without passing in the token to the task method?

Or do I have to monitor token.IsCancellationRequested in the DoIt method?

  • 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-31T10:05:59+00:00Added an answer on May 31, 2026 at 10:05 am

    By reading from here http://social.msdn.microsoft.com/Forums/da-DK/parallelextensions/thread/9f88132a-f8bd-4885-ab63-645d7b6c2127 it seems that the token is used to cancel the task BEFORE the task is “really” started, but after it has been queued.

    It’s more a way to cancel a task that’s scheduled to occur, but not started yet. Once the task is running, the only way to cancel it is cooperatively via your own checking within the method. Without this, you’d have to always start the task, then check it internally, which would add a lot of extra, unnecessary overhead

    You can even read it from Cancellation token in Task constructor: why?

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

Sidebar

Related Questions

lets say I have a class like this: public class MyClass { private String
Let's say I have something like this: $db=new PDO($dsn); $statement=$db->query('Select * from foo'); while
Let's say there is event ComboBox_SelectedIndexChange something like this private void MyComboBox_SelectedIndexChanged(object sender, EventArgs
Lets say i have a thread running like this: private boolean working = true;
Let's say I have a manager that looks something like this: public class CustomerManager
Lets say I have a set of model classes like this: public class Person
Let's say I started a thread and I have something like this: ...//initiate all
Let's say I have a MySQL table that is something like this: software table:
Let's say I have a list of words, something like this: ['The', 'Quick', 'Brown',
Possible Duplicate: C printing bits Lets say I have a uint16_t number something like:

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.