多线程应用程序中的GUI刷新会导致异常



在我的文件搜索应用程序中,刷新GUI (OnPropertyChanged) 时遇到问题

我从我所有的检查目录开始:

foreach (string folderPath in dirList) {   
    this.Search (fileSearchPattern, folderPath);
}

在这个方法中,我启动了一个后台工作人员。。。

public void Search(string fileSearchPattern, string folderPath)
{
    BackgroundWorker bw = new BackgroundWorker();
    bw.DoWork += BackgroundSearch;
    bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundSearchCompleted);
    bw.RunWorkerAsync(new string[] { fileSearchPattern, folderPath });
}

我得到了当前文件夹路径的文件列表:

private void BackgroundSearch(object sender, DoWorkEventArgs e)
{
    e.Result = new HashSet<string>(
        GetFileList(
            (e.Argument as string[])[0],
            (e.Argument as string[])[1]));
}

当我有文件列表时,我触发一个事件,将项目添加到我的结果DataTable:

void BackgroundSearchCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (this.ItemsAdded != null)
    {
        // fire event files found:
        this.ItemsAdded(e.Result as HashSet<string>);
    }
}
// -------------------------------------------------------------------------------------------

这是事件ItemsAdded的事件处理程序。在这里,我再次启动后台工作人员来获取所有文件的文件信息:

public void AddItems(HashSet<string> fileNames)
{
    if (fileNames.Count > 0)
    {
        lock (this.searchResult)
        {
            this.searchResult.BeginLoadData();
            foreach (string n in fileNames)
            {
                this.searchResult.Rows.Add(
                    new object[] {null, null, null, null, null, null, 
                        n // Filename
                });
            }
            this.searchResult.EndLoadData();
        }
        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += BackgroundFileInfo;
        bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(BackgroundSearchCompleted);
        bw.RunWorkerAsync();
    }
}
private void BackgroundFileInfo(object sender, DoWorkEventArgs e)
{
    lock (this.searchResult)
    {
        foreach (DataRow row in this.searchResult.Rows)
        {
            FileInfo fi = new FileInfo(row["FULL_NAME"].ToString());
            row.BeginEdit();
            row["FILE_NAME"] = fi.Name;
            row["DIRECTORY_NAME"] = fi.DirectoryName;
            row["ATTRIB"] = fi.Attributes;
            row["CREATION"] = fi.CreationTime.ToShortDateString() + " " +
                fi.CreationTime.ToShortTimeString();
            row["LAST_WRITE"] = fi.LastWriteTime.ToShortDateString() + " " +
                fi.LastWriteTime.ToShortTimeString();
            row["LAST_ACCESS"] = fi.LastAccessTime.ToShortDateString() + " " +
                fi.LastAccessTime.ToShortTimeString();
            row.EndEdit();
        }
        this.searchResult.AcceptChanges();
        }
    }
}

当获取我的文件信息完成后,我想刷新我的GUI,但这里我得到一个异常"实例为空"(或类似的东西(

void BackgroundFileInfoCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    lock (this.searchResult)
    {
        OnPropertyChanged("SearchResult"); // <<== HERE I HAVE AN EXCEPTION!!!
    }
}

这个错误背后的原因是什么?

这可能是因为OnPropertyChanged("SearchResult"(试图更新某个dispather所拥有的数据绑定控件。

尝试使用Dispather.CurrentDispather.BeginInvoke

最新更新