ProcessRunner Class
Runs a child process with the Process class, waits for it to exit, and returns its outcome.
This type is not available in the netstandard and .NET Framework builds of Louis, because their BCL has no Process.WaitForExitAsync.
public sealed class ProcessRunner : IProcessRunnerRemarks
An instance has no state, so one instance can serve a whole program, and any number of runs at the same time.
A run has one of two modes, which ProcessRunOptions.InheritStandardStreams selects. In capture mode, the default, the runner redirects the standard streams of the child. In inherited mode, the child shares the standard streams of the current process.
In capture mode, the runner reads the standard output and the standard error of the child line by line, both at the same time, so that the child never blocks on a full pipe. The run ends when the child has exited and both streams have ended. A process that the child started, and that still holds one of the streams, delays the end of the run. The child gets no console window, even when the current process has no console.
In inherited mode, the runner reads nothing. While the child runs, the runner keeps Ctrl-C from ending the current process: the child receives the same Ctrl-C and decides how to shut down, and the runner returns its exit code. Inherited mode is of no use in a program without a console.
When the CancellationToken of a run is signaled before the run ends, the runner throws an OperationCanceledException. When the child is still running, the runner first kills it, and every process it started. When the child has exited, the runner kills nothing, and stops waiting for the streams to end.
The runner kills with Process.Kill, which leaves some processes running: on Linux, a process that runs as another user, and on Windows, an elevated process. The runner does not wait for such a process, and throws as if the kill were complete.
A child that cannot start throws what Process.Start throws, usually a Win32Exception.
See also
- Implements
Constructors
| Name | Description |
|---|---|
| ProcessRunner() |
ProcessRunner()
Available in: net10.0net11.0(not available in the other target frameworks of the package)
public ProcessRunner()No documentation is available.
Methods
| Name | Description |
|---|---|
| RunAsync(string, IEnumerable<string>, ProcessRunOptions?, CancellationToken) | Asynchronously runs a child process and waits for it to exit. |
RunAsync(string, IEnumerable<string>, ProcessRunOptions?, CancellationToken)
Available in: net10.0net11.0(not available in the other target frameworks of the package)
Asynchronously runs a child process and waits for it to exit.
public Task<ProcessResult> RunAsync(string executable, IEnumerable<string> arguments, ProcessRunOptions? options = null, CancellationToken cancellationToken = default)Parameters
executablestring- The name or the path of the program to run.
argumentsIEnumerable<string>- The arguments of the program. Each item reaches the program as one argument, whatever characters it holds.
optionsProcessRunOptions?- The options of the run, or
nullto use the default value of every option. cancellationTokenCancellationToken- A
CancellationTokenthat, when signaled, ends the run.
Returns
Task<ProcessResult> A Task<TResult> whose result describes the outcome of the run.
Exceptions
ArgumentNullExceptionexecutableisnull. - or -argumentsisnull.ArgumentExceptionargumentsholds anullitem.ProcessFailedException- The process exited with a code other than 0, and
ProcessRunOptions.ThrowOnNonZeroExitCodeistrue. OperationCanceledExceptioncancellationTokenwas signaled before the run ended.