Simple wrapper around System.Diagnostics.Process to expose it as a System.Threading.Tasks.Task<ProcessResults>
Includes support for cancellation, timeout (via cancellation), and exposes the standard output, standard error, exit code, and run time of the process.
To install into your project:
PM> Install-Package RunProcessAsTask
NOTE: if you need to handle stdout/stderr as they happen (while the process is still running), you may want to use Process directly or look at the CliWrap project
Task<ProcessResults> processResults = ProcessEx.RunAsync("git.exe", "pull").Result;
Console.WriteLine("Exit code: " + processResults.ExitCode);
Console.WriteLine("Run time: " + processResults.RunTime);
Console.WriteLine("{0} lines of standard output", processResults.StandardOutput.Length);
foreach (var output in processResults.StandardOutput)
{
Console.WriteLine("Output line: " + output);
}
Console.WriteLine("{0} lines of standard error", processResults.StandardError.Length);
foreach (var error in processResults.StandardError)
{
Console.WriteLine("Error line: " + error);
}
public async Task RunCommandWithTimeout(string filename, string arguments, TimeSpan timeout)
{
var processStartInfo = new ProcessStartInfo
{
FileName = filename,
Arguments = arguments,
};
try
{
using (var cancellationTokenSource = new CancellationTokenSource(timeout))
{
var processResults = await ProcessEx.RunAsync(processStartInfo, cancellationTokenSource.Token);
}
}
catch (OperationCanceledException)
{
Console.WriteLine("Timeout of {0} hit while trying to run {1} {2}", timeout, filename, arguments);
}
}
public async Task ShowLastMatchingCommit(string regex)
{
var logProcessResults = await ProcessEx.RunAsync("git.exe", "log --pretty=oneline --all -n 1 -G" + regex);
if (logProcessResults.ExitCode != 0) return;
var stdoutSplit = logProcessResults.StandardOutput[0].Split(new[] { ' ' }, 2);
var commitHash = stdoutSplit[0];
var commitMessage = stdoutSplit[1];
Console.WriteLine("Last commit matching {0} was {1} and had commit message {2}", regex, commitHash, commitMessage);
var showProcessResults = await ProcessEx.RunAsync("git.exe", "show --pretty=fuller " + commitHash);
foreach (var stdoutLine in showProcessResults.StandardOutput)
{
Console.WriteLine("git show output: " + stdoutLine);
}
}