所以我的理解是,每當使用實現IDisposable的類時,它的父類也需要實現IDisposable接口。 (使用FileSystemWatcher的FileWatcher)處理FileSystemWatcher
因此,當使用FileSystemWatcher時,處理FileSystemWatcher的正確方法是什麼?我希望FileWatcher在應用程序關閉之前不要丟棄/(觀看)。
我會使用負責任的所有者模式?(嘗試/最終)還是其他? 我的FileWatcher是否也實現了IDisposable?我將無法使用{},因爲此fileWatcher應該在整個應用程序運行期間觀察文件更改。處理這種情況的正確方法是什麼?
public class FileWatcher : IFileWatcher
{
private FileSystemWatcher watcher;
public event EventHandler<EventArgs> SettingsChanged;
public FileWatcher(bool start)
{
this.RegisterForChanges();
}
public void OnChanged(object source, EventArgs e)
{
if (this.SettingsChanged != null)
{
this.SettingsChanged(source, new EventArgs());
}
}
private void RegisterForChanges()
{
/// more code here etc
...
this.watcher = new FileSystemWatcher
{
Path = directory,
NotifyFilter =
NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName,
Filter = fileName
};
// Add event handlers.
this.watcher.Changed += this.OnChanged;
// Begin watching.
this.watcher.EnableRaisingEvents = true;
}
'FileSystemWatcher'是非常討厭的,以使用 - 非常不可靠。如果你在它周圍重用一個[現有的包裝器](http://idcomlog.codeplex.com/SourceControl/latest#IdComLog.Reactive/FileSystem.cs),你會有更多的時間。鏈接的使用Rx,但我相信你可以找到更多。 –