Hello, I am writing from hopelessness, I wrote a program that checks a lot of folders on the server and searches for the file changelog.xml, takes data from it, first I wrote a linear method that works fine, but slowly, in connection with this I redid it for parallel execution, I chose the value 256 as standard for the number of threads.

When processing the code below, the link variable, for reasons that are incomprehensible to me, has the values ​​of the folders already passed. Those. as a result, the same folder is checked several times.

private void FinderParallelV3(int idT) { Random rnd = new Random(); int gb = int.Parse(GB.Text), gb_ = int.Parse(GB_.Text), gs = int.Parse(GS.Text), gs_ = int.Parse(GS_.Text), v = int.Parse(V.Text), v_ = int.Parse(V_.Text); int step = 1, iCount = 0; string filePath = string.Format("./FOUND/listG{0}-{1}g{2}-{3}-v{4}-{5}-step{6}.txt", gb, gb_, gs, gs_, v, v_, step); Array arr = getDiapasonNew(v, v_); try { if (!File.Exists(filePath)) File.Create(filePath).Close(); } catch { MessageBox.Show("Не удалось создать файл " + filePath); } int tCount = MIN_THREADS; threadsCombo.Invoke(new Action(() => { tCount = int.Parse(threadsCombo.SelectedItem.ToString()); })); if (tCount >= (v_ - v)) { MessageBox.Show("Данные настройки параметров поиска недопустимы." + Environment.NewLine + "Поиск не будет запущен.", "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } int a = gb; int b = gs; for (int t = 0; t <= tCount - 1; t++) { addThread(new Thread(() => { int id = t; bool DoMain = true; int c = int.Parse(arr.GetValue(0, t - 1).ToString()), c_ = int.Parse(arr.GetValue(1, t - 1).ToString()); string fw = null, model = null, type = null, link = null; int size = 0; int a_thread = a, b_thread = b; Task<bool> tryget = null; while (DoMain) { try { iterCount.Invoke(new Action(() => { iterCount.Text = string.Format("Количество итераций : {0}", ++iCount); })); } catch { Console.WriteLine("ERROR ON 154"); } for (int f = 1; f <= 4; f++) { link = string.Format("http://update.hicloud.com:8180/TDS/data/files/p3/s15/G{0}/g{1}/v{2}/f{3}/full/changelog.xml", a_thread, b_thread, c, f); tryget = TryGetV3(link); tryget.Wait(); if (tryget.Result) { tryget.Dispose(); fw = xmlGetModel(link); size = xmlGetSize(link); try { foreach (string s in readModels) { if (fw.Contains(s)) { model = s; break; } } if (size > 1500000000) { type = "FULL"; } else { type = "OTA"; } dataGridView1.Invoke(new Action(() => { dataGridView1.Rows.Add(model, fw, type, link, "Download", a_thread, b_thread, c, f, size); })); firmwareCount.Invoke(new Action(() => { firmwareCount.Text = string.Format("Найдено прошивок : {0}", dataGridView1.Rows.Count); })); } catch { Console.WriteLine("ERROR ON 188"); } } link = null; } try { //dataGridView1.Invoke(new Action(() => { RemoveDuplicate(this.dataGridView1); })); File.AppendAllText(filePath, string.Format(@"{0},{1},{2},{3},{4},{5}", dataGridView1.Rows[5].Cells[dataGridView1.Columns.Count].Value.ToString(), dataGridView1.Rows[6].Cells[dataGridView1.Columns.Count].Value.ToString(), dataGridView1.Rows[7].Cells[dataGridView1.Columns.Count].Value.ToString(), dataGridView1.Rows[8].Cells[dataGridView1.Columns.Count].Value.ToString(), dataGridView1.Rows[1].Cells[dataGridView1.Columns.Count].Value.ToString(), dataGridView1.Rows[9].Cells[dataGridView1.Columns.Count].Value.ToString()) + Environment.NewLine); } catch { } //V WHILE ++c; if (c < c_) DoMain = true; else DoMain = false; fw = null; model = null; type = null; link = null; size = 0; tryget = null; } }), t); } } 

Advise any solution to this problem, maybe to optimize this scribbling. Thank you. PS Do not judge strictly, the first program in C #

  • I would add a dictionary and check whether the folder was checked in it before scanning the folder, but you need to use a thread-safe collection. Or, as an option, first make a list of all paths, and then split them into packets and process these packets in different streams. - Alexsandr Ter
  • it is not even about a dozen of hundreds of options, but about hundreds of thousands of possible locations, getDiapasonNew in this method I break the V range into packets, i.e. equal amount of V per stream - just_koala

0