2013-02-28 108 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