如何在不使用交叉线程的情况下在backgroundworker中使用textBox.Text?

时间:2016-03-23 20:49:31

标签: c# .net winforms

这是dowork事件中的一行

List<string> Result = SearchInFile(CurrentFileWithPath, textBox2.Text);

这是整个dowork事件代码

object[] CurrentStatus;
        private void _FileProcessingWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            int countmore = 0;
            try
            {
                //object[] CurrentStatus = new object[5];
                DirectoryInfo[] MySubDirectories = (DirectoryInfo[])e.Argument;
                for (int i = 0; i < MySubDirectories.GetLength(0); i++)
                {
                    DirectoryInfo MySubDirectory = MySubDirectories[i];

                    List<FileInfo> l = new List<FileInfo>();
                    CountFiles(MySubDirectory, l);
                    CurrentStatus = new object[6];
                    int totalFiles = l.Count;
                    CurrentStatus[3] = i.ToString();
                    countmore += totalFiles;
                    CurrentStatus[4] = countmore;
                    _FileProcessingWorker.ReportProgress(0, CurrentStatus);

                    string CurrentDirectory = "Current Directory: " + MySubDirectory.Name;

                    foreach (FileInfo MyFile in l)
                    {
                        CurrentStatus = new object[6];
                        if (_FileProcessingWorker.CancellationPending)
                        {
                            e.Cancel = true;
                            return;
                        }

                        if (MyFile.Extension.ToLower() == ".cs" || MyFile.Extension.ToLower() == ".vb")
                        {
                            string CurrentFile = "Current File: " + MyFile.Name;
                            string CurrentFileWithPath = MyFile.FullName;

                            CurrentStatus[0] = CurrentDirectory;
                            CurrentStatus[1] = CurrentFile;
                            _FileProcessingWorker.ReportProgress(0, CurrentStatus);

                            List<string> Result = SearchInFile(CurrentFileWithPath, textBox2.Text);

                            if (Result != null && Result.Count > 0)
                            {
                                CurrentStatus[2] = Result;
                                _FileProcessingWorker.ReportProgress(0, CurrentStatus);
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                return;
            }  
        }

我没有收到错误,但是我在textBox2文本中看到了一个断点: Text =函数求值需要运行所有线程。

如果没有其他方法可以使用交叉线程吗?

0 个答案:

没有答案