Problem with BackgroundWorker component?

Ken Ekholm 151 Reputation points
2022-04-07T11:10:27.513+00:00

When I pushed the Transfer button a second time, I get the this error message, System.InvalidOperationException: 'This BackgroundWorker is currently busy and cannot run multiple tasks concurrently.' Can someone help me with this problem?

 private void buttonTransfer_Click(object sender, EventArgs e)
 {
       if (backgroundWorker1.IsBusy)
       {
            backgroundWorker1.WorkerSupportsCancellation = true
``          backgroundWorker1.CancelAsync();
            MessageBox.Show("Running!");
        }

         richTextBoxViewStatus.Clear();                  
         backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.RunWorkerAsync();
}




private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            backgroundWorker1.WorkerSupportsCancellation = true;
            backgroundWorker1.CancelAsync();
        }


private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
      do some work
}
Developer technologies C#
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. Jack J Jun 25,296 Reputation points
    2022-04-08T07:38:02.997+00:00

    @Ken Ekholm , Welcome to Microsoft Q&A, based on your code, I can reproduce your problem.

    Please don't start the BackgroundWorker twice because One BackgroundWorker can only run One task.

    If you want to run many background tasks at the same time, you could create multiple BackgroundWorker objects.

    I make a code examaple for two background tasks and you could refer to it.

    191294-image.png

    Hope this could help you.

    Update code:

    192998-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Ken Ekholm 151 Reputation points
    2022-04-08T17:55:24.177+00:00

    Is it possible to "reuse" the same backgroundworker instead for creating a new one?


  3. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2022-04-08T21:40:28.457+00:00

    Try the following, fill in the TODO sections

    private void RunButton_Click(object sender, EventArgs e)
    {
        var bw = new BackgroundWorker { WorkerReportsProgress = true };
        bw.DoWork += (_, _) => {/* TODO  */ };
        bw.ProgressChanged += (_, _) => {/* TODO  */ };
        bw.RunWorkerCompleted += (_, _) => {/* TODO  */ };
        bw.RunWorkerAsync();
    }
    
    0 comments No comments

  4. Ken Ekholm 151 Reputation points
    2022-04-10T18:53:18.93+00:00

    This code require language C# 9.0 and I have for the moment language C# 7.3.
    How do I write the same code for langauge C# 7.3?

    I have tried to update to language C# 9.0 without success.


  5. Ken Ekholm 151 Reputation points
    2022-04-10T19:16:27.93+00:00

    Edit: I managed to update to language C# 9.0.
    But it does not work to "reuse" the same backgroundworker with this code.

    private void RunButton_Click(object sender, EventArgs e)
    {
    var bw = new BackgroundWorker { WorkerReportsProgress = true };
    bw.DoWork += (_, ) => {/* TODO */ };
    bw.ProgressChanged += (
    , ) => {/* TODO */ };
    bw.RunWorkerCompleted += (
    , _) => {/* TODO */ };
    bw.RunWorkerAsync();
    }

    Is it possible to resolve this?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.