Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 14 additions & 31 deletions src/BenchmarkDotNet/Extensions/ProcessExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using BenchmarkDotNet.Detectors;
using BenchmarkDotNet.Engines;
using BenchmarkDotNet.Environments;
using BenchmarkDotNet.Helpers;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Loggers;
using BenchmarkDotNet.Portability;
Expand All @@ -13,9 +14,6 @@
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading.Tasks;


namespace BenchmarkDotNet.Extensions
{
Expand Down Expand Up @@ -220,43 +218,28 @@ private static (int exitCode, string output) RunProcessAndReadOutput(string file
Arguments = arguments,
RedirectStandardOutput = true,
RedirectStandardError = false,
UseShellExecute = false
UseShellExecute = false,
CreateNoWindow = true,
},
EnableRaisingEvents = true
};

var stdout = new StringBuilder();

var tcsExited = new TaskCompletionSource<bool>();
var tcsStdout = new TaskCompletionSource<bool>();

process.Exited += (_, __) => tcsExited.TrySetResult(true);
process.OutputDataReceived += (_, e) =>
{
if (e.Data != null)
stdout.AppendLine(e.Data);
else
tcsStdout.TrySetResult(true);
};
using var processOutputReader = new AsyncProcessOutputReader(process, readStandardError: false);
using var consoleExitHandler = new ConsoleExitHandler(process, NullLogger.Instance);

process.Start();
process.BeginOutputReadLine();
processOutputReader.BeginRead();

var tasks = Task.WhenAll(tcsExited.Task, tcsStdout.Task);
if (tasks.Wait(timeout))
return (process.ExitCode, stdout.ToString());

// Handle timeout
try
bool isSuccess = process.WaitForExit((int)timeout.TotalMilliseconds);
if (!isSuccess)
{
process.KillTree();
}
catch
{
// Ignore exception
processOutputReader.CancelRead();
consoleExitHandler.KillProcessTree();

return (process.HasExited ? process.ExitCode : -1, default);
}

return (process.HasExited ? process.ExitCode : -1, default);
processOutputReader.StopRead();
return (process.ExitCode, processOutputReader.GetOutputText());
}

private static int RunProcessAndIgnoreOutput(string fileName, string arguments, TimeSpan timeout)
Expand Down