Skip to content
forked from Cysharp/ProcessX

Simplify call an external process with the async streams in C# 8.0.

License

Notifications You must be signed in to change notification settings

bbarry/ProcessX

 
 

Repository files navigation

ProcessX

CircleCI

ProcessX simplifies call an external process with the aync streams in C# 8.0 without complex Process code. You can receive standard output results by await foreach, it is completely asynchronous and realtime.

image

Getting Started

Install library from NuGet that support from .NET Standard 2.0.

PM> Install-Package ProcessX

Main API is only Cysharp.Diagnostics.ProcessX.StartAsync and throws ProcessErrorException when error detected.

  • Simple, only write single string command like the shell script.
  • Asynchronous, by C# 8.0 async streams.
  • Manage Error, handling exitcode and stderror.
using Cysharp.Diagnostics; // using namespace

// async iterate.
await foreach (string item in ProcessX.StartAsync("dotnet --info"))
{
    Console.WriteLine(item);
}

// receive buffered result(similar as WaitForExit).
string[] result = await ProcessX.StartAsync("dotnet --info").ToTask();

// when ExitCode is not 0 or StandardError is exists, throws ProcessErrorException
try
{
    await foreach (var item in ProcessX.StartAsync("dotnet --foo --bar")) { }
}
catch (ProcessErrorException ex)
{
    // int .ExitCode
    // string[] .ErrorOutput
    Console.WriteLine(ex.ToString());
}

Reference

to Cancel, you can use WithCancellation of IAsyncEnumerable.

await foreach (var item in ProcessX.StartAsync("dotnet --info").WithCancellation(cancellationToken))
{
    Console.WriteLine(item);
}

timeout, you can use CancellationTokenSource(delay).

using (var tcs = new CancellationTokenSource(TimeSpan.FromSeconds(1)))
{
    await foreach (var item in ProcessX.StartAsync("dotnet --info").WithCancellation(tcs.Token))
    {
        Console.WriteLine(item);
    }
}

ProcessX.StartAsync overloads, you can set workingDirectory, environmentVariable, encoding.

StartAsync(string command, string? workingDirectory = null, IDictionary<string, string>? environmentVariable = null, Encoding? encoding = null)
StartAsync(string fileName, string? arguments, string? workingDirectory = null, IDictionary<string, string>? environmentVariable = null, Encoding? encoding = null)
StartAsync(ProcessStartInfo processStartInfo)

Task<string[]> ToTask(CancellationToken cancellationToken = default)

Competitor

License

This library is under the MIT License.

About

Simplify call an external process with the async streams in C# 8.0.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 100.0%