Background worker Is busy error and I want to implement in progress bar

146 Views Asked by At
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.

1

There are 1 best solutions below

0
jmcilhinney On

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();
}