/// <summary> /// Executes a command script. /// </summary> /// <param name="runner">The object invoking the command (will be notified /// on completion or failure)</param> /// <param name="cmdFile">The command file to run</param> /// <param name="args">Any command line arguments for the script</param> /// <exception cref="ArgumentNullException">If the supplied <paramref name="runner"/> /// is null</exception> /// <exception cref="InvalidAsynchronousStateException">If a previous /// run has not yet completed</exception> public void RunCommand(IBatchRunner runner, string cmdFile, string args) { // Disallow an attempt to run something if a prior call is still executing. if (IsBusy) { throw new InvalidAsynchronousStateException(); } if (runner == null) { throw new ArgumentNullException(); } // I don't see how anything here could lead to an exception, // but who knows... try { m_Runner = runner; m_CommandFile = cmdFile; richTextBox.ScrollBars = RichTextBoxScrollBars.None; ProcessStartInfo processStartInfo = new ProcessStartInfo(); processStartInfo.WorkingDirectory = Path.GetDirectoryName(cmdFile); processStartInfo.FileName = cmdFile; processStartInfo.Arguments = args; processStartInfo.UseShellExecute = false; processStartInfo.RedirectStandardOutput = true; processStartInfo.RedirectStandardError = true; processStartInfo.CreateNoWindow = true; stdoutWorker.RunWorkerAsync(processStartInfo); } catch (Exception ex) { m_Runner = null; runner.RunCompleted(ex); } }
/// <summary> /// Executes a command script. /// </summary> /// <param name="runner">The object invoking the command (will be notified /// on completion or failure)</param> /// <param name="cmdFile">The command file to run</param> /// <param name="args">Any command line arguments for the script</param> /// <exception cref="ArgumentNullException">If the supplied <paramref name="runner"/> /// is null</exception> /// <exception cref="InvalidAsynchronousStateException">If a previous /// run has not yet completed</exception> public void RunCommand(IBatchRunner runner, string cmdFile, string args) { // Disallow an attempt to run something if a prior call is still executing. if (IsBusy) throw new InvalidAsynchronousStateException(); if (runner == null) throw new ArgumentNullException(); // I don't see how anything here could lead to an exception, // but who knows... try { m_Runner = runner; m_CommandFile = cmdFile; richTextBox.ScrollBars = RichTextBoxScrollBars.None; ProcessStartInfo processStartInfo = new ProcessStartInfo(); processStartInfo.WorkingDirectory = Path.GetDirectoryName(cmdFile); processStartInfo.FileName = cmdFile; processStartInfo.Arguments = args; processStartInfo.UseShellExecute = false; processStartInfo.RedirectStandardOutput = true; processStartInfo.RedirectStandardError = true; processStartInfo.CreateNoWindow = true; stdoutWorker.RunWorkerAsync(processStartInfo); } catch (Exception ex) { m_Runner = null; runner.RunCompleted(ex); } }
private void stdoutWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { Debug.Assert(m_Runner != null); richTextBox.ScrollBars = RichTextBoxScrollBars.Vertical; m_Runner.RunCompleted(e.Error); }