所以我的理解是,每当使用实现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,但我相信你可以找到更多。 –