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
)?或者我應該在私人領域中引用它以防止它被垃圾收集?
我可以找到沒有文件說'EnableRaisingEvents'對垃圾回收有任何影響。我認爲需要一個本地字段來確保'FileSystemWatcher'沒有被垃圾回收。 – Lukazoid 2012-02-03 00:32:47
我已經編輯了我的答案,並顯示了一個示例,顯示如果EnableRaisingEvents爲false,FileSystemWatcher將被垃圾收集。 – 2012-02-03 01:02:13
感謝你的這一點,我確實希望這個功能在MSDN上有記錄,它看起來很容易結束一些懸掛的'FileSystemWatcher's。 – Lukazoid 2012-02-03 01:14:27