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 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
  • 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. 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

Ask A Question

Stats

  • Questions 81k
  • Answers 82k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer If you are asking can you alter the buffering of… May 11, 2026 at 4:34 pm
  • Editorial Team
    Editorial Team added an answer In the Data Flow, the OLE DB Command can be… May 11, 2026 at 4:34 pm
  • Editorial Team
    Editorial Team added an answer You can also use str_relpace with the $count parameter: $str… May 11, 2026 at 4:34 pm

Related Questions

In several modern programming languages (including C++, Java, and C#), the language allows integer
While fleshing out a hypothetical domain model, I have found myself wondering whether the
If you have a situation where a TCP connection is potentially too slow and
Consider a normal customer-orders application based on MVC pattern using WinForms. The view part

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.