コード例 #1
0
        protected sealed override bool run()
        {
            var startInfo = StartInfo.GetValue();

            using (var process = new Process()
            {
                StartInfo = startInfo
            })
            {
                if (DataReceiveAction != null)
                {
                    process.OutputDataReceived += (_, e) => DataReceiveAction.GetValue()(e);
                }

                if (ErrorReceiveAction != null)
                {
                    process.ErrorDataReceived += (_, e) => ErrorReceiveAction.GetValue()(e);
                }

                if (!process.Start())
                {
                    return(false);
                }

                if (startInfo.RedirectStandardOutput)
                {
                    process.BeginOutputReadLine();
                }

                if (startInfo.RedirectStandardError)
                {
                    process.BeginErrorReadLine();
                }

                if (Timeout == null)
                {
                    process.WaitForExit();
                }
                else
                {
                    process.WaitForExit(Timeout.GetValue());
                }

                if (startInfo.RedirectStandardInput)
                {
                    process.CancelOutputRead();
                }

                if (startInfo.RedirectStandardError)
                {
                    process.CancelErrorRead();
                }

                var exitCode = process.ExitCode;

                if (ExitCode != null)
                {
                    ExitCode.SetValue <int>(exitCode);
                }

                if (StartTime != null)
                {
                    StartTime.SetValue <DateTime>(process.StartTime);
                }

                if (ExitTime != null)
                {
                    ExitTime.SetValue <DateTime>(process.ExitTime);
                }

                if (ResultConverter == null)
                {
                    return(exitCode == 0);
                }
                else
                {
                    return(ResultConverter.Convert(exitCode));
                }
            }
        }