0

在我的文件搜索应用程序中,我在刷新 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]));
}

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

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!!!
    }
}

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

4

1 回答 1

0

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

尝试Dispather.CurrentDispather.BeginInvoke

于 2013-02-28T09:37:13.330 回答