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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:06:03+00:00 2026-06-04T00:06:03+00:00

I have the following setup in my code that utilizes the TPL: One field

  • 0

I have the following setup in my code that utilizes the TPL:

  • One field in my class: private CancellationTokenSource _cancellationTokenSource;
  • This CancellationTokeSource gets instatiated every time I create a TPL task that uses that particular cancellationtoken

The actualy TPL tasks look like this:

var dataRetrievalTask = new Task<List<myType>>(() =>
            {
                // retrieve data and do something
                foreach (var a in retrievalMethod())
                {
                    if (_cancellationTokenSource.Token.IsCancellationRequested)
                        _cancellationTokenSource.Token.ThrowIfCancellationRequested();

                        // do something if not cancelled
                    }
                }

                return filledListOfMyType;

            }, _cancellationTokenSource.Token);

            // define what shall happen if data retrievel finished without any problems
            var writingLoadedDataToGridTask = dataRetrievalTask.ContinueWith(task =>
            {
              // do something in case we ran to completion without error
            }, _cancellationTokenSource.Token, TaskContinuationOptions.OnlyOnRanToCompletion, currentScheduler);

            // what to do in case cancellation was performed
            var loadingDataCancelledTask = dataRetrievalTask.ContinueWith(task =>
                              {
                                someLabel.Text = "Data retrieval canceled.";
                              },_cancellationTokenSource.Token, TaskContinuationOptions.OnlyOnCanceled, currentScheduler);

            // what to do in case an exception / error occured
            var loadingDataFaulted = dataRetrievalTask.ContinueWith(task =>
                {
                    someLabel.Text = string.Format("Data retrieval ended with an Error.");
                }, _cancellationTokenSource.Token, TaskContinuationOptions.OnlyOnFaulted, currentScheduler);

            // when any of the continuation tasks ran through, reset ui controls / buttons etc
            Task.Factory.ContinueWhenAny(new[] { writingLoadedDataToGridTask, loadingDataCancelledTask, loadingDataFaulted }, task =>
            {
              // reset controls and all that
            }, _cancellationTokenSource.Token, TaskContinuationOptions.None, currentScheduler);


            dataRetrievalTask.Start();

Now my problem is that when the _cancellationTokenSource.Cancel() method is called somewhere (in a Cancel-button’s .Click event handler) that particular loadingDataCancelledTask’s body/method isn’t called.

What am I doing wrong here? I am using and handing over the same _cancellationTokenSource.Token instance… and everything else (the ‘writingLoadedDataToGridTask’ and ‘loadingDataFaulted’ tasks & the following ‘Task.Factory.ContinueWhenAny(new[] { writingLoadedDataToGridTask, loadingDataCancelledTask, loadingDataFaulted }, task => …’ block) do actually work. Only cancellation does not. Does anyone see/know why?

  • 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-04T00:06:05+00:00Added an answer on June 4, 2026 at 12:06 am

    Your cancellation continuation is being cancelled because it uses the same cancellation token.

    If you think about it it makes total sense: When you say “I want to cancel all processing” you actually get what you ask for. All processing stops, including updating the UI.

    The solution is to not use the cancellation token for the cancel, error and ContinueWhenAny continuations. That way these continuations always run.

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

Sidebar

Related Questions

I have the following setup: class.staff.php This defines many variables, the one I'm working
I have this following setup, a textarea named with some data in it that
I have the following domain model setup using EF code first: public class User
I have the following setup: Code on my local machine (OS X) shared as
I have the following piece of code: // setup the AJAX request var pageRequest
I have following models setup in my Django application class School(models.Model): name = models.TextField()
I have the following code: [SetUp] public void SetMeUp() { Mapper.CreateMap<SourceObject, DestinationObject>(); } [Test]
I have the following setup: Sharepoint 2010 with SQL Server and Office 2010. This
My Project Setup I have the following project setup: \program.jar \images\logo.png In my code,
I have the following ul setup: <div id=sidebar> <ul id=themenu class=sf-menu sf-vertical> <li> <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.