2013-02-28 98 views
0

在我的文件搜索应用程序,我有一个问题,刷新GUI (OnPropertyChanged)GUI刷新导致异常

我用我所有的检查迪尔斯开始:

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, 但在这里我得到一个异常“的一个实例为null “(或类似的东西)

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

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

+0

你应该真的使用一个单独的对象来保持锁。只需创建一个“对象锁定”并锁定它,而不是在你的'searchResult'变量上。 – 2013-02-28 09:24:59

+1

异常具有类型和堆栈跟踪。请将它们完全添加到您的qeustion中。没有他们,我们无法猜测任何事情。问题不在于你发布的代码。 – 2013-02-28 09:25:48

+0

你也可以只发布整个错误,不清楚你发布的内容有什么问题。 – 2013-02-28 09:25:50

回答

0

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

尝试使用Dispather.CurrentDispather.BeginInvoke

+0

我已经实现了Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate {OnPropertyChanged(“SearchResult”); });但错误不会消失 – peter70 2013-02-28 13:23:23