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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T00:34:10+00:00 2026-06-12T00:34:10+00:00

I am using a Task to read some data from the Database etc. Let’s

  • 0

I am using a Task to read some data from the Database etc. Let’s assume I can’t change the Dataaccess-API/-Layer.

This dataaccess might sometimes need some time (network-traffic etc.). It will be loaded everey time a user changes a selected item or changes the filter that shows a subset of the aviable data.

At the end I have a little example of my Task-Start-Method.

My Question is: If the user changes the filter/selection while the task is still running, how can I stop it from running? When using the cancellation token it will finish (as I am not using a “big” loop in the task, I can’t just check every iteration for .IsCancelled.

My idea was to use the return type of the Task to fill the SelectableVolts and check the returning Task on IsCancelled before assigning the new value. But how to do this for an async Task?

// added code at the bottom of this question

UPDATE: After getting comments like “I am not totally sure what your question is asking for, but this should help you get a feel for some avalible options.” I will try to clarify my problem a little bit. At least I hope so 😉

  1. User selects object in datagrid
  2. ViewModel needs data and asks method/class/foo for data.
  3. Task(A) is started
  4. Task(A) is still working. User selects different object/row.
  5. Steps 1, 2 and 3 are repeated. So Task(A) should be cancelled/stopped and a new Task(B) starts.
  6. When Task(B) is finished its data should be displayed. In no way Task(A)s data should be made available.

So the question is: How to achieve steps 5 and 6 in a correct way.

Full Code:

    private CancellationToken cancelTokenOfFilterTask;
    private CancellationTokenSource cancelTokenSourceOfFilterTask;

private void FilterVolts()
{
    if (IsApplicationWorking)
    {
        cancelTokenSourceOfFilterTask.Cancel();
    }

    // Prepare CancelToken
    cancelTokenSourceOfFilterTask = new CancellationTokenSource();
    cancelTokenOfFilterTask = cancelTokenSourceOfFilterTask.Token;

    IsApplicationWorking = true;
    if (SelectableVolts != null && SelectableVolts.Count >= 0)
    {
        VoltThatIsSelected = null;
        SelectableVolts.Clear();
    }

    Task voltLoadTask = null;
    voltLoadTask = Task.Factory.StartNew<List<SelectableVoltModel>>(() => {
       VoltsLoader loader = new VoltsLoader();
       Thread.Sleep(2000);
       var listOfVolts = loader.LoadVoltsOnFilter(_sourceOfCachableData, ChosenFilter);
            return listOfVehicles;
        }, cancelTokenOfFilterTask).ContinueWith(listSwitcher =>
        {
            switch (listSwitcher.Status)
            {
                case TaskStatus.RanToCompletion:
                    SelectableVolts = new ObservableCollection<SelectableVoltsModel>(listSwitcher.Result);
                    IsApplicationWorking = false;
                    break;
                case TaskStatus.Canceled:
                    Console.WriteLine("Cancellation seen"); // Gets never called
                    break;           
                default:
                    break;
            }
        });
    }

Somehow when I call this Method more than once, all will run in TaskStatus.RanTocompletion how can this be possible? Am I doing someting wrong with the cancel-token?

  • 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-12T00:34:11+00:00Added an answer on June 12, 2026 at 12:34 am

    Answer: I think I found a solution, I have to reset the CancellationTokenSource after every cancel-request.

    Here is my updated Code based on the accepted answer. If there’s something wrong I am happy about any information 🙂

        private CancellationToken cancelTokenOfFilterTask;
        private CancellationTokenSource cancelTokenSourceOfFilterTask = new CancellationTokenSource();
    
        private void FilterVolts()
        {
            if (IsApplicationWorking)
            {
                cancelTokenSourceOfFilterTask.Cancel();
            }
    
            // Prepare CancelToken
            cancelTokenOfFilterTask = cancelTokenSourceOfFilterTask.Token;
    
            IsApplicationWorking = true;
            if (SelectableVolts != null && SelectableVolts.Count >= 0)
            {
                VoltThatIsSelected = null;
                SelectableVolts.Clear();
            }
    
            Task voltsLoadTask = null;
            voltsLoadTask = Task.Factory.StartNew<List<SelectableVoltsModel>>(() => {
                VehicleLoader loader = new VehicleLoader();
                Thread.Sleep(2000); // just for testing, so the task runs a "long" time
                var listOfVolts = loader.LoadVoltsOnFilter(_sourceOfCachableData, ChosenFilter);
                return listOfVolts;
            }, cancelTokenOfFilterTask).ContinueWith(listSwitcher =>
            {
                switch (listSwitcher.Status)
                {
                    case TaskStatus.RanToCompletion:
                        SelectableVolts = new ObservableCollection<SelectableVoltModel>(listSwitcher.Result);
                        IsApplicationWorking = false;
                        break;
                    case TaskStatus.Canceled:
                        cancelTokenSourceOfFilterTask = new CancellationTokenSource(); // reset Source
                        break;
                    default:
                        break;
                }
            });
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a background thread to read some data from my database.The data that
I'm using the Task class from JavaFx's API to perform network operations. The class
I have a ruby script which will read data from some CSV file and
I have a task to perform an HttpWebRequest using Task<WebResponse>.Factory.FromAsync(req.BeginGetRespone, req.EndGetResponse) which can obviously
I wrote a window service that performs some task using the threads.Now i am
I have to create an app that will read in some info from a
I am building and designing a (mostly) read-only interface to some data. I'll be
I have a long running Async Task that sends some data to my server
I've read akka documentation and can't produce clean understanding of thread interaction while using
I have a task at hand, read a database that was created with HTML-OS,

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.