private FileSystemWatcher EnsureWatcher(string channelName) { FileSystemWatcher watcher; if (watcherList.TryGetValue(channelName, out watcher)) { return(watcher); } lock (lockObj) { if (watcherList.TryGetValue(channelName, out watcher)) { return(watcher); } var folder = XDIOStreamBroadcaster.GetChannelDirectory(channelName); watcher = new FileSystemWatcher(folder, "*.msg") { NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite }; watcher.Changed += OnMessageReceived; watcherList.Add(channelName, watcher); } return(watcher); }
/// <summary> /// Provides a thread safe method to lookup/create a instance of FileSystemWatcher for a particular channel. /// </summary> /// <param name = "channelName"></param> /// <returns></returns> private FileSystemWatcher EnsureWatcher(string channelName) { FileSystemWatcher watcher; // try to get a reference to the watcher used for the current watcher if (!watcherList.TryGetValue(channelName, out watcher)) { // if no watcher then lock the list lock (lockObj) { // whilst locked double check if the item has been added since the lock was applied if (!watcherList.TryGetValue(channelName, out watcher)) { // create a new watcher for the given channel, by default this is not enabled. string folder = XDIOStreamBroadcaster.GetChannelDirectory(channelName); watcher = new FileSystemWatcher(folder, "*.msg") { NotifyFilter = NotifyFilters.CreationTime | NotifyFilters.LastWrite }; watcher.Changed += OnMessageReceived; watcherList.Add(channelName, watcher); } } } return(watcher); }