// call to update/create watchers on joutnal and UI. Do it with system stopped public void SetupWatchers(string[] stdfolders) { System.Diagnostics.Debug.Assert(ScanThread == null); // double check we are not scanning. List <int> watchersinuse = new List <int>(); foreach (string std in stdfolders) // setup the std folders { watchersinuse.Add(CheckAddPath(std)); } foreach (var cmdr in EDCommander.GetListCommanders()) // setup any commander folders { if (!cmdr.ConsoleCommander && cmdr.JournalDir.HasChars()) // not console commanders, and we have a path { watchersinuse.Add(CheckAddPath(cmdr.JournalDir)); // try adding } } List <int> del = new List <int>(); for (int i = 0; i < watchers.Count; i++) // find any watchers not in the watchersinuse list { if (!watchersinuse.Contains(i)) { del.Add(i); } } for (int j = 0; j < del.Count; j++) { int wi = del[j]; System.Diagnostics.Trace.WriteLine(string.Format("Delete watch on {0}", watchers[wi].WatcherFolder)); JournalMonitorWatcher mw = watchers[wi]; mw.StopMonitor(); // just in case watchers.Remove(mw); StatusReader sw = statuswatchers[wi]; statuswatchers.Remove(sw); } }
private int CheckAddPath(string p) // return index of watcher (and therefore status watcher as we add in parallel) { try { string path = Path.GetFullPath(p); // make path normalised. this can throw, so we need to protect and reject the path if (Directory.Exists(path)) // sanity check in case folder disappeared { int present = watchers.FindIndex(x => x.WatcherFolder.Equals(path, StringComparison.InvariantCultureIgnoreCase)); if (present < 0) // and we are not watching it, add it { System.Diagnostics.Trace.WriteLine(string.Format("New watch on {0}", path)); JournalMonitorWatcher mw = new JournalMonitorWatcher(path); watchers.Add(mw); StatusReader sw = new StatusReader(path); statuswatchers.Add(sw); present = watchers.Count - 1; } else { System.Diagnostics.Trace.WriteLine(string.Format("Existing watch on {0}", path)); } return(present); } } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Watcher path exception" + ex); } return(-1); }