示例#1
0
        /// <summary>
        /// Run the specified command in a separate process capturing the output
        /// and error streams if any
        /// </summary>
        /// <param name="startInfo"></param>
        /// <param name="output"></param>
        /// <param name="error"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static ProcessOutput Run(this ProcessStartInfo startInfo, StringBuilder output = null, StringBuilder error = null, int timeout = 600000)
        {
            output = output ?? new StringBuilder();
            error  = error ?? new StringBuilder();
            ProcessOutputCollector receiver = new ProcessOutputCollector(d => output.Append(d), e => error.Append(e));

            return(Run(startInfo, receiver, timeout));
        }
示例#2
0
        public static ProcessOutput Run(this string command, bool promptForAdmin, ProcessOutputCollector outputCollector, int timeout = 600000)
        {
            ValidateCommand(command);
            GetExeAndArguments(command, out string exe, out string arguments);
            ProcessStartInfo startInfo = CreateStartInfo(promptForAdmin);

            startInfo.FileName  = command;
            startInfo.Arguments = arguments;
            return(Run(startInfo, outputCollector, timeout));
        }
示例#3
0
        /// <summary>
        /// Run the specified exe with the specified arguments, executing the specified onExit
        /// when the process completes.  This method will block if a timeout is specified, it will
        /// not block if timeout is null.
        /// </summary>
        /// <param name="exe"></param>
        /// <param name="arguments"></param>
        /// <param name="onExit"></param>
        /// <param name="onStandardOut"></param>
        /// <param name="onErrorOut"></param>
        /// <param name="promptForAdmin"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static ProcessOutput Run(this string exe, string arguments, EventHandler onExit, Action <string> onStandardOut = null, Action <string> onErrorOut = null, bool promptForAdmin = false, int?timeout = null)
        {
            ProcessStartInfo startInfo = CreateStartInfo(promptForAdmin);

            startInfo.FileName  = exe;
            startInfo.Arguments = arguments;
            ProcessOutputCollector receiver = new ProcessOutputCollector(onStandardOut, onErrorOut);

            return(Run(startInfo, onExit, receiver, timeout));
        }
示例#4
0
 private static void WaitForExit(ProcessOutputCollector output, int?timeout, ref int exitCode, ref bool timedOut, Process process, AutoResetEvent outputWaitHandle, AutoResetEvent errorWaitHandle)
 {
     if (process.WaitForExit(timeout.Value) &&
         outputWaitHandle.WaitOne(timeout.Value) &&
         errorWaitHandle.WaitOne(timeout.Value))
     {
         exitCode        = process.ExitCode;
         output.ExitCode = exitCode;
         process.Dispose();
     }
     else
     {
         output.StandardError.AppendLine();
         output.StandardError.AppendLine("Timeout elapsed prior to process completion");
         timedOut = true;
     }
 }
示例#5
0
        /// <summary>
        /// Run the specified command in a separate process capturing the output
        /// and error streams if any
        /// </summary>
        /// <param name="startInfo"></param>
        /// <param name="onExit"></param>
        /// <param name="output"></param>
        /// <param name="timeout"></param>
        /// <returns></returns>
        public static ProcessOutput Run(this ProcessStartInfo startInfo, EventHandler onExit, ProcessOutputCollector output = null, int?timeout = null)
        {
            int  exitCode = -1;
            bool timedOut = false;

            output = output ?? new ProcessOutputCollector();

            Process process = new Process()
            {
                EnableRaisingEvents = true
            };

            if (onExit != null)
            {
                process.Exited += onExit;
            }
            process.StartInfo = startInfo;
            AutoResetEvent outputWaitHandle = new AutoResetEvent(false);
            AutoResetEvent errorWaitHandle  = new AutoResetEvent(false);

            process.OutputDataReceived += (sender, e) =>
            {
                if (e.Data == null)
                {
                    outputWaitHandle.Set();
                }
                else
                {
                    output.DataHandler(e.Data);
                }
            };
            process.ErrorDataReceived += (sender, e) =>
            {
                if (e.Data == null)
                {
                    errorWaitHandle.Set();
                }
                else
                {
                    output.ErrorHandler(e.Data);
                }
            };

            process.Start();
            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            if (timeout != null)
            {
                WaitForExit(output, timeout, ref exitCode, ref timedOut, process, outputWaitHandle, errorWaitHandle);
                return(new ProcessOutput(output.StandardOutput.ToString(), output.StandardError.ToString(), exitCode, timedOut));
            }
            else
            {
                process.Exited += (o, a) =>
                {
                    Process p = (Process)o;
                    output.ExitCode = p.ExitCode;
                    p.Dispose();
                };
            }

            return(new ProcessOutput(process, output.StandardOutput, output.StandardError));
        }
示例#6
0
 /// <summary>
 /// Run the specified command in a separate process capturing the output
 /// and error streams if any
 /// </summary>
 /// <param name="startInfo"></param>
 /// <param name="output"></param>
 /// <param name="timeout"></param>
 /// <returns></returns>
 public static ProcessOutput Run(this ProcessStartInfo startInfo, ProcessOutputCollector output = null, int?timeout = null)
 {
     return(Run(startInfo, null, output, timeout));
 }
示例#7
0
        public static ProcessOutput RunAndWait(this ProcessStartInfo info, Action <string> standardOut = null, Action <string> errorOut = null, int timeOut = 60000)
        {
            ProcessOutputCollector output = new ProcessOutputCollector(standardOut, errorOut);

            return(Run(info, output, timeOut));
        }