public Task <int> StartSearchAnsyc(SearchData data, DataGridView dataGrid) { return(Task.Run(async() => { if (data.UseMultiThreading) { if (dataGrid.RowCount < Environment.ProcessorCount * Multiplier) { data.UseMultiThreading = false; await search.ThreadSearch(data, dataGrid, descriptor); goto cont; } int first_index = 0, second_index, step; SearchData[] datas = new SearchData[Environment.ProcessorCount * Multiplier]; step = dataGrid.RowCount / datas.Length; second_index = step; for (int i = 0; i < Environment.ProcessorCount * Multiplier; i++) { datas[i] = new SearchData(); datas[i].first_index = first_index; datas[i].second_index = second_index; datas[i].str = data.str; datas[i].numberOfColumn = data.numberOfColumn; datas[i].RegistrON = data.RegistrON; datas[i].UseWordWhole = data.UseWordWhole; first_index += step; second_index += step; } if (first_index < dataGrid.RowCount) { data.first_index = first_index; data.second_index = dataGrid.RowCount; search.ThreadSearch(data, dataGrid, descriptor); } for (int i = 0; i < datas.Length - 1; i++) { search.ThreadSearch(datas[i], dataGrid, descriptor); } await search.ThreadSearch(datas[datas.Length - 1], dataGrid, descriptor); } else { await search.ThreadSearch(data, dataGrid, descriptor); } cont: int result = resultsOfSearch; resultsOfSearch = 0; return result; })); }
private void StartSearch_Click(object sender, EventArgs e) { CloSE.Visible = false; StartSearch.Visible = false; MessageAboutSearch.Visible = true; MessageAboutSearch.Focus(); switch (owner) { case Users usersForm: SearchData data = new SearchData(0, usersForm.DataUserGrid.RowCount, SearchPoly.SelectedIndex == 0 ? 5 : SearchPoly.SelectedIndex - 1, KeyWord.Text, WordOllChekBox.Checked, RegistrOn.Checked, MultiThreadCheckBox.Checked); usersForm.Searching(data); break; case Books booksForm: data = new SearchData(0, booksForm.DataBooksGrid.RowCount, SearchPoly.SelectedIndex == 0 ? 5 : SearchPoly.SelectedIndex - 1, KeyWord.Text, WordOllChekBox.Checked, RegistrOn.Checked, MultiThreadCheckBox.Checked); booksForm.Searching(data); break; } }
public void Searching(SearchData data) { if (data.UseMultiThreading) { if (DataBooks.RowCount < Environment.ProcessorCount * 2) { ThreadPool.QueueUserWorkItem(arg => ThreadSearch(data)); return; } int first_index = 0, second_index, step; Thread[] threads = new Thread[Environment.ProcessorCount * 2]; SearchData[] datas = new SearchData[threads.Length]; step = DataBooks.RowCount / datas.Length; second_index = step; for (int i = 0; i < datas.Length; i++) { datas[i] = new SearchData(); datas[i] = data; datas[i].first_index = first_index; datas[i].second_index = second_index; threads[i] = new Thread(ThreadSearch); first_index += step; second_index += step; } if (second_index < DataBooks.RowCount) { data.first_index = second_index; data.second_index = DataBooks.RowCount; ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadSearch), data); } for (int i = 0; i < datas.Length - 1; i++) { threads[i].Start(datas[i]); } threads[datas.Length - 1].Name = "LAST"; threads[datas.Length - 1].Start(datas[datas.Length - 1]); } else { ThreadPool.QueueUserWorkItem(arg => ThreadSearch(data)); } }
public Task ThreadSearch(SearchData inf, DataGridView dataGrid, Form descriptor) { return(Task.Factory.StartNew(() => { int flag = 0; string str = inf.str; int countOfResults = 0; if (inf.RegistrON) { if (inf.UseWordWhole) { string[] str_words = str.Split(' '); string[] buf_words; if (inf.numberOfColumn == 5) { for (int i = inf.first_index; i < inf.second_index; i++) { for (int j = 0; j < 3; j++) { buf_words = dataGrid.Rows[i].Cells[j].Value.ToString().Split(' '); if (buf_words.Length == str_words.Length) { for (int indx = 0; indx < str_words.Length; indx++) { if (str_words[indx] != buf_words[indx]) { goto lalala; } } flag++; j = 3; break; } if (buf_words.Length < str_words.Length) { continue; } for (int indx = 0; indx < buf_words.Length - str_words.Length + 1; indx++) { if (buf_words[indx] == str_words[0]) { for (int I = 0; I < str_words.Length; I++) { if (str_words[I] != buf_words[indx + I]) { goto сontinue; } } flag++; j = 3; } break; сontinue:; } lalala:; } if (flag == 0) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); continue; } else { countOfResults++; flag = 0; } } } else { for (int i = inf.first_index; i < inf.second_index; i++) { buf_words = dataGrid.Rows[i].Cells[inf.numberOfColumn].Value.ToString().Split(' '); if (buf_words.Length == str_words.Length) { for (int indx = 0; indx < str_words.Length; indx++) { if (str_words[indx] != buf_words[indx]) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); goto lalala; } } countOfResults++; continue; } if (buf_words.Length < str_words.Length) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); continue; } for (int indx = 0; indx < buf_words.Length - str_words.Length + 1; indx++) { if (buf_words[indx] == str_words[0]) { for (int I = 0; I < str_words.Length; I++) { if (str_words[I] != buf_words[indx + I]) { goto сontinue; } } flag++; countOfResults++; goto Break; } сontinue:; } Break: if (flag == 0) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); } flag = 0; lalala:; } } } else { string bufferWord = null; if (inf.numberOfColumn == 5) { for (int i = inf.first_index; i < inf.second_index; i++) { for (int j = 0; j < 3; j++) { bufferWord = dataGrid.Rows[i].Cells[j].Value.ToString(); if (bufferWord.Length < str.Length) { continue; } if (!bufferWord.Contains(str)) { continue; } flag++; countOfResults++; break; } if (flag == 0) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); } flag = 0; } } else { for (int i = inf.first_index; i < inf.second_index; i++) { bufferWord = dataGrid.Rows[i].Cells[inf.numberOfColumn].Value.ToString(); if (bufferWord.Length < str.Length) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); continue; } if (!bufferWord.Contains(str)) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); continue; } countOfResults++; } } } } else { string bufferWord = null; str = str.ToLower(); // и так со всеми строками if (inf.UseWordWhole) { string[] str_words = str.Split(' '); string[] buf_words; if (inf.numberOfColumn == 5) { for (int i = inf.first_index; i < inf.second_index; i++) { for (int j = 0; j < 3; j++) { buf_words = dataGrid.Rows[i].Cells[j].Value.ToString().ToLower().Split(' '); if (buf_words.Length == str_words.Length) { for (int indx = 0; indx < str_words.Length; indx++) { if (str_words[indx] != buf_words[indx]) { goto lalala; } } flag++; j = 3; break; } if (buf_words.Length < str_words.Length) { continue; } for (int indx = 0; indx < buf_words.Length - str_words.Length + 1; indx++) { if (buf_words[indx] == str_words[0]) { for (int I = 0; I < str_words.Length; I++) { if (str_words[I] != buf_words[indx + I]) { goto сontinue; } } flag++; j = 3; } goto Break; сontinue:; } lalala:; } Break: if (flag == 0) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); continue; } else { countOfResults++; flag = 0; } } } else { if (inf.numberOfColumn != 2) { for (int i = inf.first_index; i < inf.second_index; i++) { buf_words = dataGrid.Rows[i].Cells[inf.numberOfColumn].Value.ToString().ToLower().Split(' '); if (buf_words.Length == str_words.Length) { for (int indx = 0; indx < str_words.Length; indx++) { if (str_words[indx] != buf_words[indx]) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); goto lalala; } } countOfResults++; continue; } if (buf_words.Length < str_words.Length) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); continue; } for (int indx = 0; indx < buf_words.Length - str_words.Length + 1; indx++) { if (buf_words[indx] == str_words[0]) { for (int I = 0; I < str_words.Length; I++) { if (str_words[I] != buf_words[indx + I]) { goto сontinue; } } flag++; countOfResults++; } break; сontinue:; } if (flag == 0) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); } flag = 0; lalala:; } } else { for (int i = inf.first_index; i < inf.second_index; i++) { bufferWord = dataGrid.Rows[i].Cells[2].Value.ToString().ToLower(); if (bufferWord != str) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); } else { countOfResults++; } } } } } else { if (inf.numberOfColumn == 5) { for (int i = inf.first_index; i < inf.second_index; i++) { for (int j = 0; j < 2; j++) { bufferWord = dataGrid.Rows[i].Cells[j].Value.ToString().ToLower(); if (bufferWord.Length < str.Length) { continue; } if (!bufferWord.Contains(str)) { continue; } flag++; countOfResults++; break; } if (str == dataGrid.Rows[i].Cells[2].Value.ToString()) { flag++; } if (flag == 0) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); } else { flag = 0; } } } else { if (inf.numberOfColumn != 2) { for (int i = inf.first_index; i < inf.second_index; i++) { bufferWord = dataGrid.Rows[i].Cells[inf.numberOfColumn].Value.ToString().ToLower(); if (bufferWord.Length < str.Length) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); continue; } if (!bufferWord.Contains(str)) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); continue; } countOfResults++; } } else { for (int i = inf.first_index; i < inf.second_index; i++) { bufferWord = dataGrid.Rows[i].Cells[2].Value.ToString(); if (bufferWord != str) { descriptor.Invoke((Action) delegate { dataGrid.Rows[i].Visible = false; }); continue; } countOfResults++; } } } } } taskEnded(countOfResults); })); }
private void ThreadSearch(Object data) { SearchData inf = data as SearchData; string str = inf.str; int countOfResults = 0; if (inf.RegistrON) { if (inf.UseWordWhole) { string[] str_words = str.Split(' '); string[] buf_words; if (inf.numberOfColumn == 5) { for (int i = inf.first_index; i < inf.second_index; i++) { for (int j = 0; j < 3; j++) { buf_words = DataBooks.Rows[i].Cells[j].Value.ToString().Split(' '); if (buf_words.Length == str_words.Length) { for (int indx = 0; indx < str_words.Length; indx++) { if (str_words[indx] != buf_words[indx]) { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); goto lalala; } } Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); countOfResults++; j = 3; continue; } if (buf_words.Length < str_words.Length) { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); continue; } Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); for (int indx = 0; indx < buf_words.Length - str_words.Length + 1; indx++) { if (buf_words[indx] == str_words[0]) { for (int I = 0; I < str_words.Length; I++) { if (str_words[I] != buf_words[indx + I]) { goto сontinue; } } Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); countOfResults++; j = 3; break; сontinue :; } } lalala :; } } if (!inf.UseMultiThreading) { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } else { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); if (Thread.CurrentThread.Name == "LAST") { Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } } } else { for (int i = inf.first_index; i < inf.second_index; i++) { buf_words = DataBooks.Rows[i].Cells[inf.numberOfColumn].Value.ToString().Split(' '); if (buf_words.Length == str_words.Length) { for (int indx = 0; indx < str_words.Length; indx++) { if (str_words[indx] != buf_words[indx]) { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); goto lalala; } } Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); countOfResults++; continue; } if (buf_words.Length < str_words.Length) { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); continue; } Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); for (int indx = 0; indx < buf_words.Length - str_words.Length + 1; indx++) { if (buf_words[indx] == str_words[0]) { for (int I = 0; I < str_words.Length; I++) { if (str_words[I] != buf_words[indx + I]) { goto сontinue; } } Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); countOfResults++; break; сontinue :; } } lalala :; } if (!inf.UseMultiThreading) { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } else { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); if (Thread.CurrentThread.Name == "LAST") { Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } } } } else { string bufferWord = null; if (inf.numberOfColumn == 5) { for (int i = inf.first_index; i < inf.second_index; i++) { for (int j = 0; j < 3; j++) { bufferWord = DataBooks.Rows[i].Cells[j].Value.ToString(); if (bufferWord.Length < str.Length) { continue; } if (bufferWord.Contains(str)) { countOfResults++; Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); break; } else { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); } } } if (!inf.UseMultiThreading) { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } else { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); if (Thread.CurrentThread.Name == "LAST") { Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } } } else { for (int i = inf.first_index; i < inf.second_index; i++) { bufferWord = DataBooks.Rows[i].Cells[inf.numberOfColumn].Value.ToString(); if (bufferWord.Length < str.Length) { continue; } if (bufferWord.Contains(str)) { countOfResults++; Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); continue; } else { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); } } if (!inf.UseMultiThreading) { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } else { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); if (Thread.CurrentThread.Name == "LAST") { Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } } } } } else { string bufferWord = null; str = str.ToLower(); // и так со всеми строками if (inf.UseWordWhole) { string[] str_words = str.Split(' '); string[] buf_words; if (inf.numberOfColumn == 5) { for (int i = inf.first_index; i < inf.second_index; i++) { for (int j = 0; j < 3; j++) { buf_words = DataBooks.Rows[i].Cells[j].Value.ToString().ToLower().Split(' '); if (buf_words.Length == str_words.Length) { for (int indx = 0; indx < str_words.Length; indx++) { if (str_words[indx] != buf_words[indx]) { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); goto lalala; } } Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); countOfResults++; j = 3; continue; } if (buf_words.Length < str_words.Length) { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); continue; } Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); for (int indx = 0; indx < buf_words.Length - str_words.Length + 1; indx++) { if (buf_words[indx] == str_words[0]) { for (int I = 0; I < str_words.Length; I++) { if (str_words[I] != buf_words[indx + I]) { goto сontinue; } } Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); countOfResults++; j = 3; break; сontinue :; } } lalala :; } } if (!inf.UseMultiThreading) { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } else { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); if (Thread.CurrentThread.Name == "LAST") { Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } } } else { for (int i = inf.first_index; i < inf.second_index; i++) { buf_words = DataBooks.Rows[i].Cells[inf.numberOfColumn].Value.ToString().Split(' '); if (buf_words.Length == str_words.Length) { for (int indx = 0; indx < str_words.Length; indx++) { if (str_words[indx] != buf_words[indx].ToLower()) { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); goto lalala; } } Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); countOfResults++; continue; } if (buf_words.Length < str_words.Length) { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); continue; } Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); for (int indx = 0; indx < buf_words.Length - str_words.Length + 1; indx++) { if (buf_words[indx].ToLower() == str_words[0]) { for (int I = 0; I < str_words.Length; I++) { if (str_words[I] != buf_words[indx + I].ToLower()) { goto сontinue; } } Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); countOfResults++; break; сontinue :; } } lalala :; } if (!inf.UseMultiThreading) { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } else { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); if (Thread.CurrentThread.Name == "LAST") { Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } } } } else { if (inf.numberOfColumn == 5) { for (int i = inf.first_index; i < inf.second_index; i++) { for (int j = 0; j < 3; j++) { bufferWord = DataBooks.Rows[i].Cells[j].Value.ToString().ToLower(); if (bufferWord.Length < str.Length) { continue; } if (bufferWord.Contains(str)) { countOfResults++; Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); break; } else { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); } } } if (!inf.UseMultiThreading) { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } else { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); if (Thread.CurrentThread.Name == "LAST") { Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } } } else { if (inf.numberOfColumn != 2) { for (int i = inf.first_index; i < inf.second_index; i++) { bufferWord = DataBooks.Rows[i].Cells[inf.numberOfColumn].Value.ToString().ToLower(); if (bufferWord.Length < str.Length) { continue; } if (bufferWord.Contains(str)) { countOfResults++; Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); continue; } else { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); } } if (!inf.UseMultiThreading) { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } else { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); if (Thread.CurrentThread.Name == "LAST") { Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } } } else { for (int i = inf.first_index; i < inf.second_index; i++) { bufferWord = DataBooks.Rows[i].Cells[inf.numberOfColumn].Value.ToString().ToLower(); if (bufferWord == str) { countOfResults++; Invoke((Action) delegate { DataBooks.Rows[i].Visible = true; }); continue; } else { Invoke((Action) delegate { DataBooks.Rows[i].Visible = false; }); } } if (!inf.UseMultiThreading) { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } else { Invoke((Action) delegate { countOfTrueRows += countOfResults; }); if (Thread.CurrentThread.Name == "LAST") { Invoke((Action) delegate { MessageBox.Show("Совпадений: " + countOfTrueRows.ToString(), "Поиск завершен"); }); Invoke((Action) delegate { sf.Close(); }); ////////////////////// } } } } } } }