-3
private void button1_Click(object sender, EventArgs e)
{
    //progress bar
    backgroundWorker1.RunWorkerAsync();
    progressBar1.Value = 1;
    progressBar1.Minimum = 1;
    progressBar1.Maximum = 10;
    label3.Text = "backgroundworker Task completed";

    Thread.Sleep(100);
    Application.DoEvents();
    // end of bgw
}

I am getting error on the Backgroundworker1.RunworkerAsync() where it shows an error of Backgroundworker is busy and try to use it aftertime. Is there any other method to solve. I thank in advance.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
samccxr
  • 3
  • 3
  • Your code makes little sense as it is. I suggest that you read [this](https://www.vbforums.com/showthread.php?542316-Using-the-BackgroundWorker-Component) to learn how to use a `BackgroundWorker` properly. – jmcilhinney Jan 31 '23 at 07:36
  • Just had a closer look at that old thread of mine and there's probably not as much information there as I thought. You are displaying a message in a `Label` as though you think the work is finished at that point, yet you are setting the initial state of the `ProgressBar` immediately beforehand. That's nonsense. Set up the initial state first, call `RunWorkerAsync`, do the work in the `DoWork` event handler, then update the UI after the work in the `RunWorkerCompleted` event handler. – jmcilhinney Jan 31 '23 at 07:46
  • Never ever ever ever call `Application.DoEvents()`. It is evil incarnate and will cause you more grief than relief. – Enigmativity Jan 31 '23 at 08:19
  • I would consider using a Task instead of a background worker. Stephen Cleary has a [article about using progress reporting from tasks](https://blog.stephencleary.com/2012/02/reporting-progress-from-async-tasks.html) that might be useful. – JonasH Jan 31 '23 at 08:57
  • The `BackgroundWorker` class is old and awkward. You can take a look [here](https://stackoverflow.com/questions/12414601/async-await-vs-backgroundworker/64620920#64620920 "Async/await vs BackgroundWorker") for an alternative (`Task.Run` + `await` + `IProgress`). – Theodor Zoulias Jan 31 '23 at 09:13

1 Answers1

1

When you call RunWorkerAsync, the IsBusy property will return True. The BackgroundWorker will raise its DoWork event and you do your background work in that event handler. Once that event handler completes, it will raise its RunWorkerCompleted event and you can optionally handle that event to update the UI after the background work. Once the event handlers for both events complete, the IsBusy property will return False. The whole point of that property is to tell you that the BackgroundWorker is already busy so you shouldn't call RunWorkerAsync. The solution to your problem is not call RunWorkerAsync until IsBusy is False again, e.g.

private void button1_Click(object sender, EventArgs e)
{
    if (backgroundWorker1.IsBusy)
    {
        return;
    }

    // Setup ProgressBar and whatever else here first.

    backgroundWorker1.RunWorkerAsync();
}
jmcilhinney
  • 50,448
  • 5
  • 26
  • 46