diff --git a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs
index 51fbfd2198..5413cefc3d 100644
--- a/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs
+++ b/MediaBrowser.Server.Implementations/IO/DirectoryWatchers.cs
@@ -23,7 +23,7 @@ namespace MediaBrowser.Server.Implementations.IO
///
/// The file system watchers
///
- private ConcurrentBag _fileSystemWatchers = new ConcurrentBag();
+ private ConcurrentDictionary _fileSystemWatchers = new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase);
///
/// The update timer
///
@@ -210,10 +210,17 @@ namespace MediaBrowser.Server.Implementations.IO
try
{
- newWatcher.EnableRaisingEvents = true;
- _fileSystemWatchers.Add(newWatcher);
+ if (_fileSystemWatchers.TryAdd(path, newWatcher))
+ {
+ newWatcher.EnableRaisingEvents = true;
+ Logger.Info("Watching directory " + path);
+ }
+ else
+ {
+ Logger.Info("Unable to add directory watcher for {0}. It already exists in the dictionary." + path);
+ newWatcher.Dispose();
+ }
- Logger.Info("Watching directory " + path);
}
catch (IOException ex)
{
@@ -232,9 +239,9 @@ namespace MediaBrowser.Server.Implementations.IO
/// The path.
private void StopWatchingPath(string path)
{
- var watcher = _fileSystemWatchers.FirstOrDefault(f => f.Path.Equals(path, StringComparison.OrdinalIgnoreCase));
+ FileSystemWatcher watcher;
- if (watcher != null)
+ if (_fileSystemWatchers.TryGetValue(path, out watcher))
{
DisposeWatcher(watcher);
}
@@ -251,11 +258,18 @@ namespace MediaBrowser.Server.Implementations.IO
watcher.EnableRaisingEvents = false;
watcher.Dispose();
- var watchers = _fileSystemWatchers.ToList();
+ RemoveWatcherFromList(watcher);
+ }
- watchers.Remove(watcher);
+ ///
+ /// Removes the watcher from list.
+ ///
+ /// The watcher.
+ private void RemoveWatcherFromList(FileSystemWatcher watcher)
+ {
+ FileSystemWatcher removed;
- _fileSystemWatchers = new ConcurrentBag(watchers);
+ _fileSystemWatchers.TryRemove(watcher.Path, out removed);
}
///
@@ -283,6 +297,11 @@ namespace MediaBrowser.Server.Implementations.IO
dw.EnableRaisingEvents = true;
success = true;
}
+ catch (ObjectDisposedException)
+ {
+ RemoveWatcherFromList(dw);
+ return;
+ }
catch (IOException)
{
Logger.Warn("Network still unavailable...");
@@ -501,9 +520,7 @@ namespace MediaBrowser.Server.Implementations.IO
LibraryManager.ItemAdded -= LibraryManager_ItemAdded;
LibraryManager.ItemRemoved -= LibraryManager_ItemRemoved;
- FileSystemWatcher watcher;
-
- while (_fileSystemWatchers.TryTake(out watcher))
+ foreach (var watcher in _fileSystemWatchers.Values.ToList())
{
watcher.Changed -= watcher_Changed;
watcher.EnableRaisingEvents = false;