Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#中的BackgroundWorker运行多次_C#_Multithreading_Backgroundworker - Fatal编程技术网

C#中的BackgroundWorker运行多次

C#中的BackgroundWorker运行多次,c#,multithreading,backgroundworker,C#,Multithreading,Backgroundworker,我正在编写一个程序来执行特定的任务(检查某些东西),当我使用后台工作程序(多个)时,它们会执行多次。我只希望他们执行一次 我有一个按钮,代码如下: OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "Text Documents|*.txt|All Files|*.*"; ofd.ShowDialog(); string filename = ofd.FileName; SortList(fil

我正在编写一个程序来执行特定的任务(检查某些东西),当我使用后台工作程序(多个)时,它们会执行多次。我只希望他们执行一次

我有一个按钮,代码如下:

    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "Text Documents|*.txt|All Files|*.*";
    ofd.ShowDialog();
    string filename = ofd.FileName;
    SortList(filename);
排序列表函数,将文件排序为多个列表,稍后我将用于后台工作人员

public void SortList(string file)
        {
            string[] names = System.IO.File.ReadAllLines(file);
            int list = 1;
            for (int i = 0; i < names.Length; i++)
            {
                switch (list)
                {
                    case 1:
                        l1.Add(names[i]);
                        break;
                    case 2:
                        l2.Add(names[i]);
                        break;
                    case 3:
                        l3.Add(names[i]);
                        break;
                    case 4:
                        l4.Add(names[i]);
                        break;
                    case 5:
                        l1.Add(names[i]);
                        list = 1;
                        break;
                }
                list++;
            }

            backgroundWorker1.RunWorkerAsync();
            backgroundWorker2.RunWorkerAsync();
            backgroundWorker3.RunWorkerAsync();
            backgroundWorker4.RunWorkerAsync();
        }
我已经做了一些调试,但我不知道为什么后台工作人员要多次做这些工作。是否有冲突或我做错了什么?谢谢

我使用的一个示例文件:

danbilzerian
dailywatch
fitness
fitspo
bodyofrachael
这是5个条目,背景工作人员多次做一些工作,结果如下所示:

public void CheckList(List<string> names)
{
    for (int i = 0; i < names.Count(); i++)
    {
        try
        {
            foreach (string s in names)
            {
                uu.parsePage("Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)", s);
                string[] r = { uu.followers.ToString(), uu.followingcount, uu.isVerified.ToString() };
                listView1.Invoke(new Action(() => listView1.Items.Add(uu.username).SubItems.AddRange(r)));
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        }
    }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
    CheckList(l1);
}

private void backgroundWorker2_DoWork(object sender, DoWorkEventArgs e)
{
    CheckList(l2);
}

private void backgroundWorker3_DoWork(object sender, DoWorkEventArgs e)
{
    CheckList(l3);
}

private void backgroundWorker4_DoWork(object sender, DoWorkEventArgs e)
{
    CheckList(l4);
}

问题出在检查表方法中

第一个循环的目的是什么?

在第一个循环中循环“名称”,然后在foreach中循环

由于您没有使用i计数器,因此第一个循环似乎没有用


BackgroundWorker事件不会被执行多次,为确保这一点,只需使用
Console.WriteLine(“BackGroundWork 1已启动”)并检查“输出”窗口中的输出。

为什么在同一列表中有内部循环时会有外部循环?哇。在这之后我真的觉得自己很笨。我已经修复了我的额外循环,显然它工作得很好,没有背景工作人员多次工作。非常感谢你的提醒。只有一个问题,“输出”窗口到底是什么?它位于哪里?使用Visual Studio,查看>输出。您可以在msdn上查看更多信息和比我更好的解释: