-
Notifications
You must be signed in to change notification settings - Fork 0
Executing and Working With Commands
Once you've created a command with the CommandBuilder, executing is as easy as calling Start (or StartAsync). When using Start, the initial startup of a process happens synchronously and a true/false value is returned signaling whether the process was started successfully. When using StartAsync, a Task is returned which will be completed once the process is started.
var started = command.Start(cancellationToken);
-OR-
var started = await command.StartAsync(cancellationToken);
There are two ways to access the exit code. The first is by calling WaitForExitAsync. This will return the exit code once the process has ended. The second is by accessing the ExitCode property which will be null while the process is running and will have a value after the process has terminated.
var exitCode = await command.WaitForExitAsync();
-OR-
var exitCode = command.ExitCode; //Will be Null if the process is still executing
If the .WithOutput(...) option was specified when building the command, output messages will be supplied to an internal Channel instance, allowing for asynchronous processing using the ReadOutputAsync method. If the process is terminated, enumeration of the output channel continues until all messages have been read. If the cancellation is requested for the cancellationToken supplied to the ReadOutputAsync method OR the token provided to Start(Async), enumeration will exit gracefully. Cancellation requested to the token supplied to ReadOutputAsync only cancels output processing and does not kill the process unless the same token was used as was used to start the process.
await foreach (var msg in command.ReadOutputAsync(cancellationToken))
{
if(msg.MessageType == MessageType.Error)
{
//Message came from StdErr
Console.WriteLine($"Error: {msg.Data}");
}
else
{
//Message had MessageType.Output from StdOut
Console.WriteLine($"Message: {msg.Data}");
}
}