Skip to content

A basic library to launch Processes as Cancellable Tasks

License

Notifications You must be signed in to change notification settings

codifice/ProcessStartAsync

Repository files navigation

ProcessStartAsync

Build status Build status Codacy Badge codecov NuGet

A basic library to launch Processes as Cancellable Tasks

Usage

Invoke a process and get the exit code as the result

using System.Diagnostics;

// ...

var process = new ProcessStartInfo("cmd.exe", "/c Hello World!");
var result = await process.StartAsync();
result.Should().Be(0);

Invoke a process and cancel if it doesn't complete within a set time

using System.Diagnostics;

// ...

try
{
    var process = new ProcessStartInfo("cmd.exe", "/c ping -t 127.0.0.1");
    var cts = new CancellationTokenSource();
    cts.CancelAfter(TimeSpan.FromMinutes(1));
    await process.StartAsync(cts.Token);
}
catch (TaskCanceledException)
{
    // One minute later
}

Invoke a process and capture its output in a StringBuilder

using System.Diagnostics;

// ...

var output = new StringBuilder();
var psi = new ProcessStartInfo("cmd.exe", @"/c tree \");
await psi.StartAsync((string line) => output.AppendLine(line)).ConfigureAwait(false);
return output.ToString();

About

A basic library to launch Processes as Cancellable Tasks

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages