private void _ScriptManager_OnScriptAborted(object sender, Common.ScriptEventArgs e) { DebugEx.WriteLine("COnfigurationManager : OnScriptAborted - " + e.Reason, DebugLevel.Informational); SNGScriptManager thisManager = null; if (sender is ScriptExecutor) { thisManager = ScriptingFormManager.GetScriptManager(sender as ScriptExecutor); } else if (sender is ScriptingForm) { thisManager = (sender as ScriptingForm).ScriptManager; } if (ScriptManagers.Contains(thisManager)) { lock (_lckObject) // locking is required to synchronize potential parallel calls { Invoke(new MethodInvoker(delegate { if (pbPullProgress.Value < pbPullProgress.Maximum) { pbPullProgress.Value += 1; } if (ScriptManagers.Count == 0) { EnableControls(); btnPullConfig.Enabled = true; lblOperationInProgress.Text = "Operation completed"; RefreshScriptManagers(); } btnCancelPull.Visible = !btnPullConfig.Enabled; })); } } }
/// <summary> /// Terminates a script specified by its ScriptManager /// </summary> /// <param name="thisManager"></param> private void TerminateScript(SNGScriptManager thisManager) { bool stopped = false; try { int waitTime = (int)SettingsManager.GetCurrentScriptSettings(this.SNGDataSet).ConnectTimeout.TotalMilliseconds; DebugEx.WriteLine(string.Format("Stopping script {0}...", thisManager.GetScriptName()), DebugLevel.Informational); stopped = thisManager.StopScript(waitTime); thisManager.SetScriptSaved(); DebugEx.WriteLine(string.Format("Closing form for script {0}...", thisManager.GetScriptName()), DebugLevel.Informational); this.Invoke(new MethodInvoker(delegate() { thisManager.CloseForm(); })); } catch (Exception Ex) { DebugEx.WriteLine(string.Format("CancelPullOperation : error while closing Form for <{0}> : {1}", thisManager.GetScriptName() + Ex.Message)); } if (!stopped) { Invoke(new MethodInvoker(delegate { if (pbPullProgress.Value < pbPullProgress.Maximum) { pbPullProgress.Value += 1; } })); } }
/// <summary> /// Cancels pending pull operations /// </summary> private void CancelPullOperation() { lock (_lckObject) { pbPullProgress.Value = 0; pbPullProgress.Maximum = ScriptManagers.Count; lblOperationInProgress.Text = "Cancelling operation..."; pbPullProgress.Visible = true; List <Task> terminators = new List <Task>(); for (int i = ScriptManagers.Count - 1; i >= 0; i--) { SNGScriptManager thisManager = ScriptManagers[i]; // Remove from ScriptManagers list to prevent acting on _ScriptManager_OnScriptFinished() event fired by th script // in response to StopScript() call below. // ScriptManagers.Remove(thisManager); terminators.Add(Task.Factory.StartNew(() => TerminateScript(thisManager))); } int waitTime = (int)SettingsManager.GetCurrentScriptSettings(this.SNGDataSet).ConnectTimeout.TotalMilliseconds; Task.Factory.StartNew(new Action(delegate { // wait for each task to complete for the double amount of connection timeout terminators.ForEach(t => t.Wait(2 * waitTime)); Invoke(new MethodInvoker(delegate { pbPullProgress.Visible = false; EnableControls(); btnPullConfig.Enabled = true; })); })); btnCancelPull.Visible = false; btnPullConfig.Enabled = false; } }
private void SaveScriptResults(SNGScriptManager thisManager) { int defaultColumnCount = Enum.GetNames(typeof(InputFileHeader)).Length - 1; bool errorOccurred = false; _workInProgress.Caption = "Operation in progress"; _workInProgress.Text = "Please wait while saving script results..."; _workInProgress.Run(); try { int itemCount = thisManager.GetItems().Count(); foreach (ScriptListViewItem thisItem in thisManager.GetItems()) { try { int ConfigSetTargetID = -1; if (int.TryParse(thisItem.SubItems[defaultColumnCount].Text, out ConfigSetTargetID)) { // Only save the result if item is selected for execution and result is success ! if (thisItem.HasChanged & thisItem.State == ScriptLineState.Success) { string targetConfig = thisItem.SubItems[(int)LVISubItem.CommandResult].Text; if (!string.IsNullOrEmpty(targetConfig)) { string[] configLines = targetConfig.Split("\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries); SaveConfiguration(configLines, ConfigSetTargetID); } } else { if (itemCount > 1) { DialogResult dRes = MessageBox.Show(string.Format("Can't save result for <{0}> because command result reported an error. Continue operation ?", thisItem.SubItems[1]), "Can't save result", MessageBoxButtons.YesNo, MessageBoxIcon.Information); if (dRes == DialogResult.No) { break; } } } } } catch (Exception Ex) { _workInProgress.Cancel(); MessageBox.Show(string.Format("Item not saved as an unexpected error occurred : {0}", Ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); errorOccurred = true; } } } finally { _workInProgress.Cancel(); } if (!errorOccurred) { thisManager.SetScriptSaved(); } }
private void _ScriptManager_OnScriptFinished(object sender, Common.ScriptEventArgs e) { DebugEx.WriteLine("ConfigurationManager : OnScriptFinished - " + e.Reason.ToString(), DebugLevel.Informational); SNGScriptManager thisManager = null; if (sender is ScriptExecutor) { thisManager = ScriptingFormManager.GetScriptManager(sender as ScriptExecutor); } else if (sender is ScriptingForm) { thisManager = (sender as ScriptingForm).ScriptManager; } if (ScriptManagers.Contains(thisManager)) { lock (_lckObject) // locking is required to synchronize potential parallel calls { ScriptManagers.Remove(thisManager); if (e.Reason != ScriptEventReason.UserAborted) { Invoke(new MethodInvoker(delegate() { if (pbPullProgress.Value < pbPullProgress.Maximum) { pbPullProgress.Value += 1; } DialogResult saveConfig = DialogResult.Yes; if (cbAutoSave.Checked || (saveConfig = MessageBox.Show(String.Format("Script <{0}> finished. Do you want to auto-save script results to database ?", thisManager.GetScriptName()), "Auto-save", MessageBoxButtons.YesNo, MessageBoxIcon.Question)) == System.Windows.Forms.DialogResult.Yes) { SaveScriptResults(thisManager); } if (saveConfig == DialogResult.Yes) { thisManager.CloseForm(); } if (ScriptManagers.Count == 0) { EnableControls(); btnPullConfig.Enabled = true; lblOperationInProgress.Text = "Operation completed"; RefreshScriptManagers(); } btnCancelPull.Visible = !btnPullConfig.Enabled; })); } else { Invoke(new MethodInvoker(delegate() { if (!IsDisposed && pbPullProgress.Value < pbPullProgress.Maximum) { pbPullProgress.Value += 1; } })); } } } }