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); } } }