//method to add a new watcher. Checks if the file has already been added to avoid duplicates private void addFileWatcher(IGH_DataAccess DA, string path) { if (watchers.Count > 0) { GH_FileWatcher watcher = watchers[0]; if (watcher.Path.Equals(path, StringComparison.OrdinalIgnoreCase)) { Helpers.Print(DA, "Connection already exists"); return; } else { Helpers.Print(DA, "File watcher disposed"); watcher.Dispose(); watchers.Clear(); } } GH_FileWatcher new_watcher = GH_FileWatcher.CreateFileWatcher(path, GH_FileWatcherEvents.All, new GH_FileWatcher.FileChangedSimple(fileChanged)); new_watcher.Buffer = new TimeSpan(1); //Print(new_watcher.Buffer.Ticks.ToString()); watchers.Add(new_watcher); Helpers.Print(DA, "New connection created"); }
public static void Watch(string sourcePath, OnRecompiledHandler callback) { if (syncronizedSources.ContainsKey(sourcePath)) { syncronizedSources[sourcePath].Callback = callback; return; } var watcher = GH_FileWatcher.CreateFileWatcher(sourcePath, GH_FileWatcherEvents.All, (p) => { var result = Create(sourcePath); syncronizedSources[sourcePath].Callback?.Invoke(result); }); syncronizedSources.Add(sourcePath, new SyncronizedSource(callback, watcher)); }