2012-07-26 80 views
5

在發生FileSystemWatcher.Error事件後,我不知道下一步該怎麼做。 異常可以是[比較]較小者,如從FileSystemWatcher錯誤中恢復的最佳做法是什麼?

一次在目錄

不影響觀看者的觀看過程中太多的變化,但它也可以是一個大問題 - 例如被監視的目錄被刪除,在這種情況下觀察者不再起作用。

我的問題是什麼是處理錯誤事件的最佳方式?

+2

停止使用FileSystemWatcher,它不可靠寫入輪詢服務。 – saj 2012-07-26 10:11:06

+0

我正在使用修改的FileSystemWatcher,它可以解決原始觀察者的大多數可靠性問題[溢出問題,重複事件和網絡路徑監視] – Nissim 2012-07-26 10:14:08

+0

或者檢查屏幕上繪製和顯示之前發生的事件上的文件。適用於上下文菜單。 (編輯:聽起來不錯尼西,分享?:P) – negligible 2012-07-26 10:19:30

回答

1

我只是簡單地得到內部異常類型,然後根據每個錯誤決定做什麼(重新啓動或失敗)。

所以

myWatcher.Error += new ErrorEventHandler(OnError); 

Followde通過

private static void OnError(object source, ErrorEventArgs e) 
{ 
    if (e.GetException().GetType() == typeof(InternalBufferOverflowException)) 
    { 
     // This can happen if Windows is reporting many file system events quickly 
     // and internal buffer of the FileSystemWatcher is not large enough to handle this 
     // rate of events. The InternalBufferOverflowException error informs the application 
     // that some of the file system events are being lost. 
     Console.WriteLine(("The file system watcher experienced an internal buffer overflow: " + e.GetException().Message)); 
    } 
} 
+0

我實際上通過使用阻塞隊列解決了所有的溢出問題 – Nissim 2012-07-26 10:15:05

2

的錯誤取決於肯定?

  1. 如果由於緩衝區溢出(很多更改)而導致數據太多,請執行列表目錄並獲取您所做的更改。
  2. 如果數據太多是因爲您沒有足夠快地處理FileSystemWatcher事件,請確保您正在有效地處理它。
  3. 已刪除的目錄,除了處置FileSystemWatcher之外無法對其執行任何操作,或者可能再次查看父目錄以重新創建該目錄名稱。
相關問題