简体   繁体   中英

BackgroundWorker in C# runs multiple times

I am writing a program that executes a specific task (to check for something), and when I used background workers (multiple), they execute multiple times. I only want them to execute once.

I have one button, with the following code:

    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Filter = "Text Documents|*.txt|All Files|*.*";
    ofd.ShowDialog();
    string filename = ofd.FileName;
    SortList(filename);

The sort list function, sorts the file into multiple lists, that I later on will use for background workers.

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

There is no problem in this function, I have debugged it, and the lists are splitted as they should, no extra lines etc. The problem is the background workers doing the work multiple times.

My last function is the check one, which looks like this:

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

And each background worker has a code like this:

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

I've done some debugging and I can't find out why the background workers are doing the work multiple times. Is there some conflict or anything that I have done wrong? Thanks.

An example file I use:

danbilzerian
dailywatch
fitness
fitspo
bodyofrachael

That's 5 entries, and the background workers do some of the work multiple times, which ends up looking like this:

在此输入图像描述

The problem is in the CheckList Method.

What is the purpose of the first loop?

You're Looping through "names" in the first loop, then looping through it in the foreach.

Since you're not using the i counter, it seems the first loop is useless.

The BackgroundWorker events are not being executed more than once , to be sure of that, just use Console.WriteLine("BackGroundWork 1 Started"); in the DoWork Events for each one of them and check the output in the "Output" Window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM