BackgroundWorker in C # works several times

I am writing a program that performs a specific task (to check something), and when I used background workers (several), they are executed several times. I only want them to be executed 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 several lists, which I will later 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 are no problems with this function, I debugged it, and the lists were divided as they should, no extra lines, etc. The problem is that the wallpaper workers work several times.

My last function is a check that 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 this code:

 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 did some debugging, and I can’t understand why the background workers do this work several times. Is there some kind of conflict or something that I did wrong? Thanks.

An example of the file I'm using:

 danbilzerian dailywatch fitness fitspo bodyofrachael 

These are 5 records, and background workers do some work several times, and it looks like this:

enter image description here

+4
source share
1 answer

The problem is with the CheckList method.

What is the purpose of the first cycle?

You loop through the "names" in the first loop, and then iterate over it in foreach.

Since you are not using counter i , it seems that the first loop is useless.

BackgroundWorker events are not executed more than once to verify this, just use Console.WriteLine("BackGroundWork 1 Started"); in the DoWork events for each of them and check the output in the "Exit" section of the window.

+5
source

All Articles