如何为每次迭代在for循环内运行BackgroundWorker进程? [英] How to run BackgroundWorker process inside for loop for each iteration?

查看:113
本文介绍了如何为每次迭代在for循环内运行BackgroundWorker进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在每个循环中正确地运行我的代码?

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

仅执行一次的原始代码可以完美地工作-

Original code which executes only once works perfect-

Form1.cs

private void CreateUserClick(object sender, RoutedEventArgs e)       
    {

            User parameters = new User
            {
                user = UserTextBox.Text + i,                         
            };


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

代码更改为可以执行用户名

code change to work for execute CreateUserWork 5 times in forloop which will save username

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(Workers.CreateUserWork);

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

DataWorkers.cs

DataWorkers.cs

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

    public static void StartUpLogic()
    {
        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);
    }
}

public static void CreateUserWork(object sender, DoWorkEventArgs e)
        {
//create new user
}

建议我进行正确的更改.

Suggest me correct change.

SE

推荐答案

请不要重复询问同一问题.

Please don't ask the same question more than once.

>>建议我进行正确的更改.

请勿将CreateUserWorker的同一静态实例传递给DataWorkers.CreateUserWorker方法.然后,您当然会在每次迭代中使用此实例.静态成员由类的所有实例共享.

Don't pass in the same static instance of the CreateUserWorker to the DataWorkers.CreateUserWorker method. Then you will of course be using this instance in each iteration. A static member is shared by all instances of a class. 

您需要像我在上一个线程中告诉您的那样,在循环内部创建一个新实例,并调用此特定实例的RunAsync方法:

You need to create a new instance inside the loop as I told you in a previous thread and call the RunAsync method of this particular instance:

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(Workers.CreateUserWork);
 createUserWorker.RunWorkerAsync(parameters);
    }
}

您调用静态DataWorkers.InitiateWork方法的方法实际上不会起作用,因为此方法似乎在某些静态词典中寻找BackroundWorker.当然,它不会找到在click事件中创建的BackgroundWorker 表格的处理程序.因此,您需要为要创建的每个BackgroundWorker添加一个新条目到StaticElements.InvokerButtons中,例如:

Your approach of calling the static DataWorkers.InitiateWork method won't really work since this method seems to be looking for a BackroundWorker in some static dictionary. Of course it won't find the BackgroundWorker that was created in the click event handler of the Form. So you then need to add a new entry to the StaticElements.InvokerButtons for each BackgroundWorker that you are creating, e.g.:

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();
 //add createUserWorker to the StaticElements.InvokerButtons if you require this here...
 //StaticElements.InvokerButtons.Add(createUserWorker, ...);
        createUserWorker.DoWork += new DoWorkEventHandler(Workers.CreateUserWork);
 DataWorkers.InitiateWork(sender, createUserWorker, parameters);

    }
}

希望有帮助.

请记住,通过将有用的帖子标记为答案来关闭话题,然后在遇到新问题时开始新话题.请不要在同一线程中问几个问题.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


这篇关于如何为每次迭代在for循环内运行BackgroundWorker进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆