public void ReloadScript() { var initialTargetVersion = targetVersion; int localCurrentVersion; do { localCurrentVersion = currentVersion; if (targetVersion <= localCurrentVersion) { targetVersion = localCurrentVersion + 1; } }while (currentVersion != localCurrentVersion); if (targetVersion > initialTargetVersion) { OnScriptChanged?.Invoke(this, EventArgs.Empty); } }
void watcher_Changed(object sender, FileSystemEventArgs e) { //PowerGUI fires the change event twice. To avoid crashes ignores the first change event. changeCounter++; if (changeCounter % 2 == 0) { try { this.Script = File.ReadAllText(scriptPath, Encoding.UTF8); if (OnScriptChanged != null) { OnScriptChanged.GetType().InvokeMember("", BindingFlags.InvokeMethod, null, OnScriptChanged, new object[] { this, Script }); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } }