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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:43:30+00:00 2026-06-14T05:43:30+00:00

I am trying to cancel a Task<> by calling the CancellationTokenSource.Cancel() method within the

  • 0

I am trying to cancel a Task<> by calling the CancellationTokenSource.Cancel() method within the task, but I cannot get it to work.

Here is the code I am using:

TaskScheduler ts = TaskScheduler.Current;

CancellationTokenSource cts = new CancellationTokenSource();

Task t = new Task( () =>
{
    Console.WriteLine( "In Task" );
    cts.Cancel();
}, cts.Token );

Task c1 = t.ContinueWith( antecedent =>
{
    Console.WriteLine( "In ContinueWith 1" );
}, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, ts );

Task c2 = c1.ContinueWith( antecedent =>
{
    Console.WriteLine( "In ContinueWith 2" );
}, TaskContinuationOptions.NotOnCanceled );

t.Start();

Console.ReadKey();

Environment.Exit( 1 );

This print outs:

In Task
In ContinueWith 1
In ContinueWith 2

What I expected was this:

In Task

Am I missing something here? Can tasks only be cancelled outside of the task?

  • 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-06-14T05:43:32+00:00Added an answer on June 14, 2026 at 5:43 am

    A task is only considered “cancelled” if

    • Its cancellation token is cancelled before it starts executing.
    • Its cancellation token is cancelled while it is executing and the code cooperatively observes the cancellation by throwing the OperationCancelledException (usually by the code calling cts.Token.ThrowIfCancellationRequested())

    If you added a cts.Token.ThrowIfCancellationRequested() line after the cts.Cancel() then things would behave as you expect. In your example, the cancellation takes place while the task is running, but the task doesn’t observe the cancellation, and the task’s action runs to completion. So the task is marked as “Ran to Completion”.

    You can check for the case of a task that “Ran to Completion” but was cancelled (either during or after the completion of the task) by checking the cancellation token within the continuation (cts.Token.IsCancellationRequested). Another option that sometimes helps is to use the original cancellation token as the cancellation token for the continuation (that way, if the the cancellation is not noticed by the antecedent task, it will at least be respected by the continuation)–since the TPL will mark the continuation as cancelled before it even has a chance to run.

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

Sidebar

Related Questions

Trying to detect when a user clicks Cancel but the $_POST on using var_export($_POST)
All, I am trying to cancel two concurrent HttpWebRequests using a method similar to
Trying to copy the msdn refernce here doesn't work and gives an error I
Got a little situation here where I am trying to cancel a file's upload.
I'm trying to cancel and then release a suspended timer but when I invoke
I'm trying to implement multistage animation using UIViewAnimationOptionBeginFromCurrentState to allow the user to cancel
I'm trying to let the users of my app cancel an active alarm using
I'm trying to get my Notification to not cancel when the user presses Clear
I'm trying to call a return type method from another class but I keep
I'm trying to programmatically intercept and cancel a user's attempt to check out a

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.