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

  • Home
  • SEARCH
  • 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 49617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:27:53+00:00 2026-05-10T16:27:53+00:00

Consider a hypothetical method of an object that does stuff for you: public class

  • 0

Consider a hypothetical method of an object that does stuff for you:

public class DoesStuff {     BackgroundWorker _worker = new BackgroundWorker();      ...      public void CancelDoingStuff()     {         _worker.CancelAsync();          //todo: Figure out a way to wait for BackgroundWorker to be cancelled.     } } 

How can one wait for a BackgroundWorker to be done?


In the past people have tried:

while (_worker.IsBusy) {     Sleep(100); } 

But this deadlocks, because IsBusy is not cleared until after the RunWorkerCompleted event is handled, and that event can’t get handled until the application goes idle. The application won’t go idle until the worker is done. (Plus, it’s a busy loop – disgusting.)

Others have add suggested kludging it into:

while (_worker.IsBusy) {     Application.DoEvents(); } 

The problem with that is that is Application.DoEvents() causes messages currently in the queue to be processed, which cause re-entrancy problems (.NET isn’t re-entrant).

I would hope to use some solution involving Event synchronization objects, where the code waits for an event – that the worker’s RunWorkerCompleted event handlers sets. Something like:

Event _workerDoneEvent = new WaitHandle();  public void CancelDoingStuff() {     _worker.CancelAsync();     _workerDoneEvent.WaitOne(); }  private void RunWorkerCompletedEventHandler(sender object, RunWorkerCompletedEventArgs e) {     _workerDoneEvent.SetEvent(); } 

But I’m back to the deadlock: the event handler can’t run until the application goes idle, and the application won’t go idle because it’s waiting for an Event.

So how can you wait for an BackgroundWorker to finish?


Update People seem to be confused by this question. They seem to think that I will be using the BackgroundWorker as:

BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += MyWork; worker.RunWorkerAsync(); WaitForWorkerToFinish(worker); 

That is not it, that is not what I’m doing, and that is not what is being asked here. If that were the case, there would be no point in using a background worker.

  • 1 1 Answer
  • 3 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. 2026-05-10T16:27:54+00:00Added an answer on May 10, 2026 at 4:27 pm

    If I understand your requirement right, you could do something like this (code not tested, but shows the general idea):

    private BackgroundWorker worker = new BackgroundWorker(); private AutoResetEvent _resetEvent = new AutoResetEvent(false);  public Form1() {     InitializeComponent();      worker.DoWork += worker_DoWork; }  public void Cancel() {     worker.CancelAsync();     _resetEvent.WaitOne(); // will block until _resetEvent.Set() call made }  void worker_DoWork(object sender, DoWorkEventArgs e) {     while(!e.Cancel)     {         // do something     }      _resetEvent.Set(); // signal that worker is done } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the hypothetical object hierarchy, starting with: TFruit = class(TObject) public constructor Create(Color: TColor);
Consider the following code, public class StartUp { public StartUp(String[] test){} public static void
Consider the hypothetical function repeatcall , that takes as arguments a no-args callable func
Consider the below: public class DependencyA {} public class DependencyB {} public class DependencyC
Consider the following hypothetical people management system. Suppose each Person object belong to a
Consider these classes. class Base { ... }; class Derived : public Base {
Consider these two classes: public class A { B b; public A(B b) {
Consider I have two classes. Professor and TimePerDay. public class TimePerDay { private ObservableCollection<TimeSpan>
Consider a project management app that uses git at backend. The new versions are
This question requires some hypothetical background. Let's consider an employee table that has columns

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.