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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T20:26:55+00:00 2026-06-02T20:26:55+00:00

I have a background worker which updates the GUI on a regular basis via

  • 0

I have a background worker which updates the GUI on a regular basis via ReportProgress.

The update occurs at regular intervals, every 5 seconds for example, or it could be 20 seconds. In order to perform the update at set times I send the worker process to sleep for the duration and when it wakes it updates the GUI with new information.

The worker supports cancellation, and outside of sleeping it cancels correctly.

I want to be able to invoke the cancellation during the wait period, however sending the thread to sleep makes this impossible.

I assume I’ll have to invoke a loop and check for the cancellation as part of the loop to simulate the thread sleep.

What is the best method for achieving this, my timing is completely off with my attempt.

            long counter = 0;
            long sleepfor = timelinespeed*1000;
            int timelinespeed = 10;

                while (counter != sleepfor)
                {

                    Thread.Sleep(1);

                    counter++;

                    if (bkgwk.CancellationPending)
                    {
                        cancelled = true;
                        e.Cancel = true;
                        bkgwk.Dispose();
                        break;
                    }

                }
  • 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-02T20:26:57+00:00Added an answer on June 2, 2026 at 8:26 pm

    You’re going down the wrong path.

    • A Bgw is using the ThreadPool and should therefore be used for (relatively) short actions, typically under 0.5 seconds. Keeping one around indefinitely is not recommended.
    • You should almost never use Sleep() on any thread. Certainly not for more than a few ms.
    • using Sleep(1) in a busy-wait for 5 seconds is a big waste of CPU. You are hurting your other threads. At the very least consider upping it to Sleep(100) or so. Find the max delay you will allow for your Cancel.

    As a solution: Use a Timer. You have a periodical task, use the right tool.

    Use System.Threading.Timer for ThreadPool based background work. Update the GUI through Dispatcher.Invoke().

    Use the Dispatcher.Timer to execute small (under 0.1 sec) portions of work on the Main thread. You can directly update the GUI.

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

Sidebar

Related Questions

I have a background worker which can be cancelled. The normal flows interrupt itself
I have a background worker that stops after 100 iterations. Like this: BackgroundWorker bgWorker
lets say i have a background worker in a class that perform db query
Hi all I have a BAckground worker and a Datatable. I have a timer
Scenario I have a background worker in my application that runs off and does
Hi all i have a background worker thread and some unmanaged code dlls ,
I'm having a lot of trouble with my background worker. I have a background
I have a project which downloads images in background using NSOperationQueue . It was
I have an update database operation, that has an activity, which keeps updating the
I have a background worker that is started on the main thread as shown

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.