1 Replies - 3546 Views - Last Post: 26 April 2012 - 12:20 PM

#1 v8n3t  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 46
  • Joined: 08-January 09

Wait on BackgroundWorkers to complete

Posted 26 April 2012 - 05:40 AM

Hello all,

My problem is figuring out how to wait for a BackgroundWorker to complete before continuing.

So right now, my code is the following in short/pseudo:

instantiate bgWorkerOne,
set WorkerSupportsCancellation == true,
bind to DoWork and ProgressChanged handlers,

if(!bgWorkerOne->IsBusy)
{
    bgWorkerOne->RunWorkerAsync();
}



cancel button is clicked to stop threads,
bgWorkerOne->CancelAsync();
this->textBoxOne->Text = "";



The problem with this is when the cancel/stop button is clicked in the main thread the BackgroundWorker is possibly and more than likely still processing something. So it runs the CancelAsync() function and then blanks out the text field but then throws a new value back in the text field because the BackgroundWorker was still doing something.

Now that the BackgroundWorker has truly stopped its operation, if I click the cancel/stop button again it will then blank out the textBox correctly and NOT be filled with an updated value shortly after.

So I need to figure out how to wait for my BackgroundWorker to complete its task before proceeding, how do I accomplish this? I feel I am not using ALL required aspects of the BackgroundWorker as I am only using the DoWork and ProgressChanged events. Thanks to all in advance for any help with this.

Is This A Good Question/Topic? 0
  • +

Replies To: Wait on BackgroundWorkers to complete

#2 v8n3t  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 46
  • Joined: 08-January 09

Re: Wait on BackgroundWorkers to complete

Posted 26 April 2012 - 12:20 PM

Just wanted to let everyone know that the fix to this is to use the RunWorkerCompleted eventhandler.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1