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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:58:00+00:00 2026-06-18T21:58:00+00:00

I have a time consuming task which I need to run in a separate

  • 0

I have a time consuming task which I need to run in a separate thread to avoid locking the GUI thread. As this task progresses, it updates a specific GUI control.

The catch is that the user might move to another part of the GUI before the task is over, and in that case, I have to:

  1. Cancel the ongoing task (if it is active)
  2. Wait till it’s done cancelling: this is crucial, because the time consuming task’s objective is to update a specific control. If more than one thread tries to do it at once, things might get messy.
  3. Launch the task from scratch

For a concrete example, imagine the form has two parts: one where you navigate a directory tree, and another where you display thumbnails. When the user navigates to another directory, thumbnails need to be refreshed.

First I thought of using a BackgroundWorker and an AutoResetEvent to wait for cancellation, but I must have messed something because I got deadlocked when cancelling. Then I read about TPL, which is supposed to replace BGW and more primitive mechanisms.

Can this be done easily using TPL?

  • 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-18T21:58:02+00:00Added an answer on June 18, 2026 at 9:58 pm

    A few things to note:

    • You can get a CancellationToken from a CancellationTokenSource

    • Task cancellation is a cooperative action: if your task does not periodically check the CancellationToken.IsCancellationRequested property, it doesn’t matter how many times you try to cancel the task, it will merrily churn away.

    Those things said, here’s the general idea:

    void Main()
    {
        var tokenSource = new CancellationTokenSource();
        var myTask = Task.Factory
            .StartNew(() => DoWork(tokenSource.Token), tokenSource.Token);
    
        Thread.Sleep(1000);
    
        // ok, let's cancel it (well, let's "request it be cancelled")
        tokenSource.Cancel();
    
        // wait for the task to "finish"
        myTask.Wait();
    }
    
    public void DoWork(CancellationToken token)
    {
        while(!token.IsCancellationRequested)
        {
            // Do useful stuff here
            Console.WriteLine("Working!");
            Thread.Sleep(100);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Maven project which performs a number of time consuming tests as
I have a queue onto which pending fourier transform requests (comparatively time consuming operations)
I have a time consuming task that is taking place in the doInBackground() method,
In My application have time consuming process.There fore i try to do that operation
have one time consuming step that flattens a bunch of files. basically i'd like
I have a time-consuming static C# method for creating an array (of double :s)
I have a time-consuming step to be done, if the user clicks on a
I have a function a time consuming operation that is done I want to
I have a ASP.NET MVC3 application that handles time-consuming processes (copying a large file
I have time in NSString like 15:15 I want to covert this NSString time

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.