本文整理汇总了C#中System.IO.FileSystemWatcher.?.Dispose方法的典型用法代码示例。如果您正苦于以下问题:C# FileSystemWatcher.?.Dispose方法的具体用法?C# FileSystemWatcher.?.Dispose怎么用?C# FileSystemWatcher.?.Dispose使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileSystemWatcher
的用法示例。
在下文中一共展示了FileSystemWatcher.?.Dispose方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateLibraryWatchers
private async Task CreateLibraryWatchers() {
Debug.Assert(_libWatchers != null, "Should not create watchers when suppressed");
IList<PythonLibraryPath> paths;
try {
paths = await PythonTypeDatabase.GetDatabaseSearchPathsAsync(_factory);
} catch (InvalidOperationException) {
return;
}
paths = paths
.Where(p => Directory.Exists(p.Path))
.OrderBy(p => p.Path.Length)
.ToList();
var watching = new List<string>();
var watchers = new List<FileSystemWatcher>();
foreach (var path in paths) {
if (watching.Any(p => PathUtils.IsSubpathOf(p, path.Path))) {
continue;
}
FileSystemWatcher watcher = null;
try {
watcher = new FileSystemWatcher {
IncludeSubdirectories = true,
Path = path.Path,
NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName | NotifyFilters.LastWrite
};
watcher.Created += OnChanged;
watcher.Deleted += OnChanged;
watcher.Changed += OnChanged;
watcher.Renamed += OnRenamed;
watcher.EnableRaisingEvents = true;
watching.Add(path.Path);
watchers.Add(watcher);
} catch (IOException) {
// Raced with directory deletion. We normally handle the
// library being deleted by disposing the watcher, but this
// occurs in response to an event from the watcher. Because
// we never got to start watching, we will just dispose
// immediately.
watcher?.Dispose();
} catch (ArgumentException ex) {
watcher?.Dispose();
Debug.WriteLine("Error starting FileSystemWatcher:\r\n{0}", ex);
}
}
List<FileSystemWatcher> oldWatchers;
lock (_libWatchers) {
oldWatchers = _libWatchers.ToList();
_libWatchers.Clear();
_libWatchers.AddRange(watchers);
}
foreach (var oldWatcher in oldWatchers) {
oldWatcher.EnableRaisingEvents = false;
oldWatcher.Dispose();
}
}