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

时间:2013-02-28 09:20:31

标签: c# wpf multithreading exception user-interface

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

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

1 个答案:

答案 0 :(得分:0)

这可能是因为OnPropertyChanged(“SearchResult”)试图更新某个收集所拥有的数据绑定控件。

尝试使用Dispather.CurrentDispather.BeginInvoke