public void ThredPoisk(CancellationToken token) { iFile = saveiFile; FindFile findFile = new FindFile(startPath, nameFormat, text); listFile = findFile.WriteFile(); button2.Invoke((MethodInvoker)(() => button2.Enabled = true)); for (int i = saveListFileI; i < listFile.Count; i++) { string s = listFile[i]; if (token.IsCancellationRequested) { saveiFile = iFile; saveListFileI = i; MessageBox.Show("Операция остановлена"); return; } iFile++; label4.Invoke((MethodInvoker)(() => label4.Text = "Файл обрабатывается: \n" + s)); // listBox1.Invoke((MethodInvoker)(() => listBox1.Items.Add(s))); label5.Invoke((MethodInvoker)(() => label5.Text = "Количество обработанных файлов: \n" + iFile)); // Thread.Sleep(50); // listBox1.Invoke((MethodInvoker)(() => listBox1.Refresh())); label4.Invoke((MethodInvoker)(() => label4.Refresh())); label5.Invoke((MethodInvoker)(() => label5.Refresh())); } readyListFile = findFile.FindTextInFile(); if (readyListFile.Count == 0) { timer1.Stop(); MessageBox.Show("Не было найдено ни одного файла"); button1.Invoke((MethodInvoker)(() => button1.Enabled = true)); button2.Invoke((MethodInvoker)(() => button2.Enabled = false)); button3.Invoke((MethodInvoker)(() => button3.Enabled = false)); label5.Invoke((MethodInvoker)(() => label5.Text = "Количество обработанных файлов: ")); label4.Invoke((MethodInvoker)(() => label4.Text = "Файл обрабатывается: ")); return; } DrawTree(); timer1.Stop(); label4.Invoke((MethodInvoker)(() => label4.Text = "Дерево построено!")); MessageBox.Show("Поиск завершён!"); button1.Invoke((MethodInvoker)(() => button1.Enabled = true)); button2.Invoke((MethodInvoker)(() => button2.Enabled = false)); button3.Invoke((MethodInvoker)(() => button3.Enabled = false)); }