2012-02-02 40 views
8

我正在使用FileSystemWatcher(在ASP.NET Web應用程序中)來監視文件以進行更改。觀察者設置在Singleton類的構造函數中,例如:我是否需要保留對FileSystemWatcher的引用?

private SingletonConstructor() 
{ 
    var fileToWatch = "{absolute path to file}"; 
    var fsw = new FileSystemWatcher(
     Path.GetDirectoryName(fileToWatch), 
     Path.GetFileName(fileToWatch)); 
    fsw.Changed += OnFileChanged; 
    fsw.EnableRaisingEvents = true; 
} 

private void OnFileChanged(object sender, FileSystemEventArgs e) 
{ 
    // process file... 
} 

到目前爲止一切正常。但我的問題是:

使用本地變量設置觀察器安全嗎(var fsw)?或者我應該在私人領域中引用它以防止它被垃圾收集?

回答

6

在上面的示例中,FileSystemWatcher僅因爲屬性EnableRaisingEvents設置爲true而保持有效。 Singleton類具有註冊爲FileSystemWatcher.Changed事件的事件處理程序的事實與fsw沒有任何直接關係,因此符合垃圾回收的條件。有關更多信息,請參閱Do event handlers stop garbage collection from occurring?

下面的代碼顯示與EnableRaisingEvents設置爲false,所述FileSystemWatcher對象作爲垃圾回收:一旦GC.Collect()被調用時,IsAlive屬性上WeakReferencefalse

class MyClass 
{ 
    public WeakReference FileSystemWatcherWeakReference; 
    public MyClass() 
    { 
     var fileToWatch = @"d:\temp\test.txt"; 
     var fsw = new FileSystemWatcher(
      Path.GetDirectoryName(fileToWatch), 
      Path.GetFileName(fileToWatch)); 
     fsw.Changed += OnFileChanged; 
     fsw.EnableRaisingEvents = false; 
     FileSystemWatcherWeakReference = new WeakReference(fsw); 
    } 

    private void OnFileChanged(object sender, FileSystemEventArgs e) 
    { 
     // process file... 
    } 

} 

class Program 
{ 
    static void Main(string[] args) 
    { 
     MyClass mc = new MyClass(); 
     GC.Collect(); 
     Console.WriteLine(mc.FileSystemWatcherWeakReference.IsAlive); 
    } 
} 
+0

我可以找到沒有文件說'EnableRaisingEvents'對垃圾回收有任何影響。我認爲需要一個本地字段來確保'FileSystemWatcher'沒有被垃圾回收。 – Lukazoid 2012-02-03 00:32:47

+0

我已經編輯了我的答案,並顯示了一個示例,顯示如果EnableRaisingEvents爲false,FileSystemWatcher將被垃圾收集。 – 2012-02-03 01:02:13

+0

感謝你的這一點,我確實希望這個功能在MSDN上有記錄,它看起來很容易結束一些懸掛的'FileSystemWatcher's。 – Lukazoid 2012-02-03 01:14:27

相關問題