-1

Previous answers to similar questions advised users that they may not have enough time spent in DoWork to be able to make call to ProgressChanged. But my calculations are lengthy--they take at least 30 seconds to calculate the value for each station calculation inside the loop.
Stepping through code in the debugger, DoWork does get invoked and does call ReportProgress. However, even when DoWork finishes because the loop completes, RunWorkerCompleted does not get called immediately. Here is the order of events :

  1. DoWork is invoked
  2. The main thread continues to loop while isBusy is true
  3. Even though DoWork completes the loop, it doesn't finish but the main thread continues in the while loop because isBusy and bgw.isBusy are still true. So I reset isBusy to false in the debugger. This ends the while loop of the main thread.
  4. bgw.Dispose() gets called from the main thread
  5. bgw.ProgressChanged gets called
  6. bgw.RunWorkerCompleted gets called

The only reason I started a background thread is to be able to show progress. The progress is shown in the call to SetStatusBar. If I don't start a background thread, I can show progress with a MessageBox.Show function call. But that requires the user to click OK after each of the 107 stations calculate. I'm trying to find a way to display the stations as the calculations proceed. Evidently there is not enough time to update the GUI if I just call SetStatusBar in the loop. So my question is how I can get the status messages to display during the loop instead of after the loop completes.

Here is my code:

Boolean isBusy = true;
// setting up a BackgroundWorker because the screen needs time to redraw
BackgroundWorker bgw = new BackgroundWorker();
bgw.WorkerSupportsCancellation = true;
bgw.WorkerReportsProgress = true;
bgw.DoWork += delegate
{
  do
  {
    isOK = CreateAverageRatingsComparisonCSV(stationIndex, out stationTitle);
    stationIndex++;
    count++;
    int percent = (int)(100 * count / numStations);
    if (percent == 0)
      percent = 1;
    bgw.ReportProgress(percent, stationTitle);
  } while (isOK && stationTitle.Length > 0);
};

bgw.RunWorkerCompleted += delegate
{
  isBusy = false;
};
bgw.ProgressChanged += new ProgressChangedEventHandler(bgworker_ProgressChanged); 
bgw.RunWorkerAsync();

// wait a 1/2 second at a time for BGW to finish      
while (bgw.IsBusy && isBusy)
{             
  System.Threading.Thread.Sleep(500);
}
bgw.Dispose();

//////////////////////////////////////////
private void bgworker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
  //display stationTitle
  SetStatusBar("Message", (string)e.UserState);
}//END 
1
  • 1
    Did you try to set just one breakpoint in the ProgressChanged event and let the code run? Is it possible that the problem is in SetStatusBar code?
    – Steve
    Oct 29, 2013 at 17:19

1 Answer 1

-1

You're blocking the UI thread while the BGW is running. All of the BGW's event handlers, besides DoWork, are marshaled to the UI thread. Since it's blocked, they are simply sitting there in the message queue doing nothing until your worker ends.

The entire purpose of using a BGW is defeated if you just have the UI thread sitting there blocking until it's done. You're better off just doing the work right on the UI thread instead.

You should remove the code that blocks the UI thread while the worker is working. If you want to execute code in the UI thread when the worker finishes then use the RunWorkerCompleted event.

2
  • What code is blocking the UI thread? Is it the while loop with the sleep command? Or do you mean that BGW running is marshalled to the UI thread which is blocked? What is the code that blocks the UI thread.
    – Diana Cook
    Oct 29, 2013 at 19:09
  • @DianaCook The code that has the UI thread sleep while the BGW is not done is what is blocking the UI thread.
    – Servy
    Oct 29, 2013 at 19:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.