//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"); }
private void SetupMainDirListener() { if (Directory.Exists(GH_ComponentServer.GHA_AppDataDirectory)) { var watcher = GH_FileWatcher.CreateDirectoryWatcher(GH_ComponentServer.GHA_AppDataDirectory, "*.ghpy", GH_FileWatcherEvents.Created, (sender, filePath, change) => { try { if (change == WatcherChangeTypes.Created) { if (LoadOneAddon(_gha_environment, filePath)) { GH_ComponentServer.UpdateRibbonUI(); } } } catch (Exception ex) { Global_Proc.ASSERT(Guid.Empty, "GhPython last exception boundary", ex); } }); watcher.Active = true; } }
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)); }
public SyncronizedSource(OnRecompiledHandler callback, GH_FileWatcher watcher) { Watcher = watcher; Callback = callback; }
void gh_watcher_Changed(GH_FileWatcher sender, string filename, System.IO.WatcherChangeTypes change) { sender = null; this.ExpireSolution(true); output.Add(filename +" "+ change.ToString()); }
protected override void SolveInstance(IGH_DataAccess DA) { // 1. Declare placeholder string source = null; string query = null; string indexPath = null; DataTree<string> queryResultTree = new DataTree<string>(); // 2. Abort on invalid inputs. if (!DA.GetData(0, ref source)) { return; } if (source == null) { return; } if (source.Length == 0) { return; } if (!DA.GetData(1, ref query)) { return; } if (query == null) { return; } if (query.Length == 0) { return; } if (!DA.GetData(2, ref indexPath)) { return; } if (query == null) { return; } if (query.Length == 0) { return; } gh_watcher = new GH_FileWatcher(indexPath, true, gh_watcher_Changed); // 3. Connect to source & fetch data MySqlConnection connection = new MySqlConnection(source); try { connection.Open(); } catch (Exception ex) { output.Add("Connection Failed.\n" + ex.ToString()); } try { MySqlCommand command = connection.CreateCommand(); command.CommandText = query; MySqlDataReader reader = command.ExecuteReader(); int i = 0; while (reader.Read()) { for (int j = 0; j < reader.FieldCount; j++) { queryResultTree.Add(reader.GetString(j), new GH_Path(i)); } i++; } } catch (Exception ex) { output.Add("Fetching failed.\n" + ex.ToString()); } connection.Close(); // 4. Output DA.SetDataTree(0, queryResultTree); DA.SetDataList(1, output); }