Beispiel #1
0
        /// <summary>
        /// Asynchronously run the specified process in the background with the specified arguments.
        /// The contents of the std (string) and err (binary) output streams are provided to callback functions.
        /// </summary>
        /// <param name="fileName">The path to the process to start.</param>
        /// <param name="arguments">Arguments for the process.</param>
        /// <param name="std">Lines read from standard output stream are sent to this callback.</param>
        /// <param name="err">Binary data buffers read from standard error stream are sent to this callback.</param>
        /// <param name="options">(Optional) Additional options.</param>
        /// <returns>An object containing the Process instance and helper functions.</returns>
        public static ProcessRunnerHandle RunProcess_StdString_ErrBinary(string fileName, string arguments, Action <string> std, Action <byte[]> err, ProcessRunnerOptions options = null)
        {
            ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments);

            psi.UseShellExecute        = false;
            psi.CreateNoWindow         = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError  = true;

            options?.Apply(psi);
            Process p = Process.Start(psi);

            options?.Apply(p);

            p.OutputDataReceived += (sender, e) =>
            {
                std(e.Data);
            };
            p.BeginOutputReadLine();

            return(new ProcessRunnerHandle(p)
            {
                stderrReader = new ProcessStreamBinaryReader(p.StandardError.BaseStream, err)
            });
        }
Beispiel #2
0
        /// <summary>
        /// Synchronously run the specified process in the background with the specified arguments, and return the exit code that the process returned.
        /// The contents of the std and err output streams are provided via out parameters.
        /// </summary>
        /// <param name="fileName">The path to the process to start.</param>
        /// <param name="arguments">Arguments for the process.</param>
        /// <param name="std">(out) standard output stream text</param>
        /// <param name="err">(out) standard error stream text</param>
        /// <param name="bThreadAbort">Set to true from another thread to abort.</param>
        /// <param name="options">(Optional) Additional options.</param>
        /// <returns>The exit code of the process that ran.</returns>
        public static int RunProcessAndWait(string fileName, string arguments, out string std, out string err, ref bool bThreadAbort, ProcessRunnerOptions options = null)
        {
            ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments);

            psi.UseShellExecute        = false;
            psi.CreateNoWindow         = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError  = true;

            StringBuilder sbOutput = new StringBuilder();
            StringBuilder sbError  = new StringBuilder();

            options?.Apply(psi);
            int exitCode;

            using (Process p = Process.Start(psi))
            {
                options?.Apply(p);
                p.OutputDataReceived += (sender, e) =>
                {
                    sbOutput.AppendLine(e.Data);
                };
                p.ErrorDataReceived += (sender, e) =>
                {
                    sbError.AppendLine(e.Data);
                };
                p.BeginOutputReadLine();
                p.BeginErrorReadLine();

                while (!bThreadAbort && !p.HasExited)
                {
                    p.WaitForExit(500);
                }
                if (bThreadAbort && !p.HasExited)
                {
                    p.Kill();
                }

                if (!bThreadAbort)
                {
                    // Must call WaitForExit without a timeout in order to force output buffers to be flushed.
                    p.WaitForExit();
                }

                exitCode = p.ExitCode;
            }

            std = sbOutput.ToString();
            err = sbError.ToString();

            return(exitCode);
        }
Beispiel #3
0
        /// <summary>
        /// Asynchronously run the specified process in the background with the specified arguments.
        /// The contents of the std (binary) and err (binary) output streams are provided to callback functions.
        /// </summary>
        /// <param name="fileName">The path to the process to start.</param>
        /// <param name="arguments">Arguments for the process.</param>
        /// <param name="std">Binary data buffers read from standard output stream are sent to this callback.</param>
        /// <param name="err">Binary data buffers from standard error stream are sent to this callback.</param>
        /// <param name="options">(Optional) Additional options.</param>
        /// <returns>An object containing the Process instance and helper functions.</returns>
        public static ProcessRunnerHandle RunProcess_StdBinary_ErrBinary(string fileName, string arguments, Action <byte[]> std, Action <byte[]> err, ProcessRunnerOptions options = null)
        {
            ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments);

            psi.UseShellExecute        = false;
            psi.CreateNoWindow         = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError  = true;

            options?.Apply(psi);
            Process p = Process.Start(psi);

            options?.Apply(p);

            return(new ProcessRunnerHandle(p)
            {
                stdoutReader = new ProcessStreamBinaryReader(p.StandardOutput.BaseStream, std),
                stderrReader = new ProcessStreamBinaryReader(p.StandardError.BaseStream, err)
            });
        }
Beispiel #4
0
        /// <summary>
        /// Synchronously run the specified process in the background with the specified arguments, and return the exit code that the process returned.
        /// The contents of the std and err output streams are provided to callback functions.
        /// </summary>
        /// <param name="fileName">The path to the process to start.</param>
        /// <param name="arguments">Arguments for the process.</param>
        /// <param name="std">Lines read from standard output stream are sent to this callback.</param>
        /// <param name="err">Lines read from standard error stream are sent to this callback.</param>
        /// <param name="options">(Optional) Additional options.</param>
        /// <returns>The exit code of the process that ran.</returns>
        public static int RunProcessAndWait(string fileName, string arguments, Action <ProcessRunnerOutputEventArgs> std, Action <ProcessRunnerOutputEventArgs> err, ProcessRunnerOptions options = null)
        {
            ProcessStartInfo psi = new ProcessStartInfo(fileName, arguments);

            psi.UseShellExecute        = false;
            psi.CreateNoWindow         = true;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError  = true;

            options?.Apply(psi);
            using (Process p = Process.Start(psi))
            {
                options?.Apply(p);
                Action abortCallback = () =>
                {
                    p.CloseMainWindow();
                    if (!p.WaitForExit(500))
                    {
                        p.Kill();
                    }
                };
                p.OutputDataReceived += (sender, e) =>
                {
                    std(new ProcessRunnerOutputEventArgs(e.Data, abortCallback));
                };
                p.ErrorDataReceived += (sender, e) =>
                {
                    err(new ProcessRunnerOutputEventArgs(e.Data, abortCallback));
                };
                p.BeginOutputReadLine();
                p.BeginErrorReadLine();

                while (!p.HasExited)
                {
                    p.WaitForExit(500);
                }
                p.WaitForExit();
                return(p.ExitCode);
            }
        }