コード例 #1
0
        private static Process StartProcess(ShellProcessArgs args)
        {
            var startInfo = new ProcessStartInfo()
            {
                FileName               = args.Executable,
                Arguments              = string.Join(" ", args.Arguments),
                WorkingDirectory       = args.WorkingDirectory?.FullName ?? new DirectoryInfo(".").FullName,
                RedirectStandardInput  = true,
                RedirectStandardOutput = true,
                StandardOutputEncoding = Encoding.UTF8,
                RedirectStandardError  = true,
                StandardErrorEncoding  = Encoding.UTF8,
                CreateNoWindow         = true,
                UseShellExecute        = false
            };

            if (args.EnvironmentVariables != null)
            {
                foreach (var pair in args.EnvironmentVariables)
                {
                    startInfo.EnvironmentVariables[pair.Key] = pair.Value;
                }
            }

            var process = new Process {
                StartInfo = startInfo
            };

            if (args.OutputDataReceived != null)
            {
                process.OutputDataReceived += args.OutputDataReceived;
            }

            if (args.ErrorDataReceived != null)
            {
                process.ErrorDataReceived += args.ErrorDataReceived;
            }

            process.Start();

            if (args.OutputDataReceived != null)
            {
                process.BeginOutputReadLine();
            }

            if (args.ErrorDataReceived != null)
            {
                process.BeginErrorReadLine();
            }

            return(process);
        }
コード例 #2
0
        public static ShellProcessOutput Run(ShellProcessArgs shellArgs)
        {
            Assert.IsNotNull(shellArgs);
            Assert.IsFalse(string.IsNullOrEmpty(shellArgs.Executable));

            var runOutput   = new ShellProcessOutput();
            var hasErrors   = false;
            var output      = new StringBuilder();
            var logOutput   = new StringBuilder();
            var errorOutput = new StringBuilder();

            // Prepare data received handlers
            DataReceivedEventHandler outputReceived = (sender, e) =>
            {
                LogProcessData(e.Data, output);
                logOutput.AppendLine(e.Data);
            };
            DataReceivedEventHandler errorReceived = (sender, e) =>
            {
                if (!string.IsNullOrEmpty(e.Data))
                {
                    errorOutput.AppendLine(e.Data);
                    hasErrors = true;
                }
                LogProcessData(e.Data, output);
                logOutput.AppendLine(e.Data);
            };

            // Run command in shell and wait for exit
            using (var process = StartProcess(new ShellProcessArgs
            {
                Executable = shellArgs.Executable,
                Arguments = shellArgs.Arguments.ToArray(),
                WorkingDirectory = shellArgs.WorkingDirectory,
                OutputDataReceived = outputReceived,
                ErrorDataReceived = errorReceived
            }))
            {
                var processUpdate = WaitForProcess(process, shellArgs.MaxIdleTimeInMilliseconds, shellArgs.MaxIdleKillIsAnError);
                while (processUpdate.MoveNext())
                {
                }

                var exitCode = process.ExitCode;
                if (processUpdate.Current == ProcessStatus.Killed)
                {
                    if (shellArgs.MaxIdleKillIsAnError)
                    {
                        exitCode = -1;
                    }
                    else
                    {
                        exitCode = 0;
                    }
                }

                runOutput.ExitCode      = exitCode;
                runOutput.Command       = shellArgs.Executable;
                runOutput.CommandOutput = output;
                runOutput.FullOutput    = logOutput.ToString();
                runOutput.ErrorOutput   = errorOutput.ToString();
                LogProcessData($"Process exited with code '{exitCode}'", logOutput);
                hasErrors |= (exitCode != 0);
            }

            if (hasErrors && shellArgs.ThrowOnError)
            {
                throw new Exception(errorOutput.ToString());
            }

            runOutput.Succeeded = !hasErrors;
            return(runOutput);
        }
コード例 #3
0
 public static Process RunAsync(ShellProcessArgs args)
 {
     return(StartProcess(args));
 }