Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

This BackgroundWorker is currently busy and cannot run multiple tasks concurrently inside for loop c#

$
0
0

I get this error if I click a button that starts the backgroundworker multiple times inside for loop.

{"This BackgroundWorker is currently busy and cannot run multiple tasks
concurrently."}

How do I work my code properly in each and every loop ?

Form1.cs

private void CreateUserClick(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < 5; i++)
    {
        User parameters = new User
        {
            user = UserTextBox.Text + i,
        };

        BackgroundWorker CreateUserWorker = new BackgroundWorker();

        CreateUserWorker.DoWork += new DoWorkEventHandler(DataWorkers.CreateUserWork);

        DataWorkers.InitiateWork(sender, CreateUserWorker, parameters);
    }
}

DataWorkers.cs

public class DataWorkers
{
    public static readonly BackgroundWorker CreateUserWorker = new BackgroundWorker();

    public static void HookUpWorkersLogic()
    {
        CreateAccountWorker.DoWork += CreateUserWork;
    }

    public static void InitiateWork(
        object sender,
        BackgroundWorker worker,
        WorkerParameters parameters = null)
    {
        StaticElements.InvokerButtons[worker] = new InvokerButton(sender);

        StaticElements.InvokerButtons[worker].Button.IsEnabled = false;
        StaticElements.InvokerButtons[worker].Button.Content = "Wait...";

        worker.RunWorkerAsync(parameters);
    }
}

I have made this change below in forloop, but still not wokring nor getting any exception after this change but application hangs up

BackgroundWorker createUserWorker = new BackgroundWorker();

createUserWorker.DoWork += new DoWorkEventHandler(DataWorkers.CreateUserWork);

DataWorkers.InitiateWork(sender, createUserWorker, parameters);

SE





Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>