// May not be necessary in the future. See https://github.com/dotnet/corefx/issues/12039 public async Task <int> RunAsync(ProcessSpec processSpec, CancellationToken cancellationToken) { Ensure.NotNull(processSpec, nameof(processSpec)); int exitCode; var stopwatch = new Stopwatch(); using (var process = CreateProcess(processSpec)) using (var processState = new ProcessState(process, _reporter)) { cancellationToken.Register(() => processState.TryKill()); process.OutputDataReceived += (_, a) => { if (!string.IsNullOrEmpty(a.Data)) { processSpec.OutputCapture.AddLine(a.Data); } }; process.ErrorDataReceived += (_, a) => { if (!string.IsNullOrEmpty(a.Data)) { processSpec.OutputCapture.AddLine(a.Data); } }; stopwatch.Start(); process.Start(); _reporter.Verbose($"Started '{processSpec.Executable}' with process id {process.Id}"); if (processSpec.IsOutputCaptured) { process.BeginErrorReadLine(); process.BeginOutputReadLine(); await processState.Task; } else { await processState.Task; } exitCode = process.ExitCode; stopwatch.Stop(); _reporter.Verbose($"Process id {process.Id} ran for {stopwatch.ElapsedMilliseconds}ms"); } return(exitCode); }
// May not be necessary in the future. See https://github.com/dotnet/corefx/issues/12039 public async Task <int> RunAsync(ProcessSpec processSpec, CancellationToken cancellationToken) { if (processSpec == null) { throw new ArgumentNullException(nameof(processSpec)); } int exitCode; var stopwatch = new Stopwatch(); using (Process process = CreateProcess(processSpec)) using (var processState = new ProcessState(process)) { cancellationToken.Register(() => processState.TryKill()); stopwatch.Start(); process.Start(); _reporter.Verbose($"Started '{processSpec.Executable}' with process id {process.Id}"); if (processSpec.IsOutputCaptured) { await Task.WhenAll( processState.Task, ConsumeStreamAsync(process.StandardOutput, processSpec.OutputCapture.AddLine), ConsumeStreamAsync(process.StandardError, processSpec.OutputCapture.AddLine) ); } else { await processState.Task; } exitCode = process.ExitCode; stopwatch.Stop(); _reporter.Verbose($"Process id {process.Id} ran for {stopwatch.ElapsedMilliseconds}ms"); } return(exitCode); }
// May not be necessary in the future. See https://github.com/dotnet/corefx/issues/12039 public async Task <int> RunAsync(ProcessSpec processSpec, CancellationToken cancellationToken) { if (processSpec == null) { throw new ArgumentNullException(nameof(processSpec)); } int exitCode; var stopwatch = new Stopwatch(); using (var process = CreateProcess(processSpec)) using (var processState = new ProcessState(process, _reporter)) { cancellationToken.Register(() => processState.TryKill()); var readOutput = false; var readError = false; if (processSpec.IsErrorCaptured) { readError = true; process.ErrorDataReceived += (_, a) => { if (!string.IsNullOrEmpty(a.Data)) { processSpec.ErrorCapture.AddLine(a.Data); } }; } if (processSpec.IsOutputCaptured) { readOutput = true; process.OutputDataReceived += (_, a) => { if (!string.IsNullOrEmpty(a.Data)) { processSpec.OutputCapture.AddLine(a.Data); } }; } stopwatch.Start(); process.Start(); _reporter.Verbose($"Started '{processSpec.Executable}' '{process.StartInfo.Arguments}' with process id {process.Id}"); if (readOutput) { process.BeginOutputReadLine(); } if (readError) { process.BeginErrorReadLine(); } await processState.Task; exitCode = process.ExitCode; stopwatch.Stop(); _reporter.Verbose($"Process id {process.Id} ran for {stopwatch.ElapsedMilliseconds}ms"); } return(exitCode); }