|
| 1 | +# Instances |
| 2 | +A .NET Standard `Process` wrapper with an elegant API, for both asyncronous and syncronous use, providing both Events and support for Tasks with cancellation support |
| 3 | + |
| 4 | + |
| 5 | +[](https://app.codecov.io/gh/rosenbjerg/Instances) |
| 6 | +[](https://github.com/rosenbjerg/Instances/blob/master/LICENSE) |
| 7 | +[](https://www.nuget.org/packages/instances/) |
| 8 | +[](https://www.nuget.org/packages/instances/) |
| 9 | + |
1 | 10 |
|
| 11 | + |
| 12 | +# Usage |
| 13 | +There are three ways to use this library, requiring at least 1, 2, or 3 lines of code to use. |
| 14 | + |
| 15 | +### Shortest form, supporting only few options |
| 16 | +```c# |
| 17 | +var result = await Instance.FinishAsync("dotnet", "build -c Release", cancellationToken); |
| 18 | +Console.WriteLine(result.ExitCode); |
| 19 | +// or |
| 20 | +var result = Instance.Finish("dotnet", "build -c Release"); |
| 21 | +``` |
| 22 | + |
| 23 | +### Short form, supporting more options |
| 24 | +```c# |
| 25 | +using var instance = Instance.Start("dotnet", "build -c Release"); |
| 26 | +var result = await instance.WaitForExitAsync(cancellationToken); |
| 27 | +// or |
| 28 | +using var instance = Instance.Start("dotnet", "build -c Release"); |
| 29 | +var result = instance.WaitForExit(); |
| 30 | +``` |
| 31 | + |
| 32 | +### Full form, supporting all options |
| 33 | +```c# |
| 34 | +var processArgument = new ProcessArguments("dotnet", "build -c Release"); |
| 35 | +processArgument.Exited += (_, exitResult) => Console.WriteLine(exitResult.ExitCode); |
| 36 | +processArgument.OutputDataReceived += (_, data) => Console.WriteLine(data); |
| 37 | +processArgument.ErrorDataReceived += (_, data) => Console.WriteLine(data); |
| 38 | + |
| 39 | +using var instance = processArgument.Start(); |
| 40 | + |
| 41 | +var result = await instance.WaitForExitAsync(cancellationToken); |
| 42 | +// or |
| 43 | +var result = instance.WaitForExit(); |
| 44 | +``` |
| 45 | + |
| 46 | + |
| 47 | +## Features |
| 48 | +```c# |
| 49 | +using var instance = Instance.Start("dotnet", "build -c Release"); |
| 50 | + |
| 51 | +// send input to process' standard input |
| 52 | +instance.SendInput("Hello World"); |
| 53 | + |
| 54 | +// stop the process |
| 55 | +instance.Kill(); |
| 56 | + |
| 57 | +// access process output |
| 58 | +foreach (var line in instance.OutputData) |
| 59 | + Console.WriteLine(line); |
| 60 | +// and error data easily while the process is running |
| 61 | +foreach (var line in instance.ErrorData) |
| 62 | + Console.WriteLine(line); |
| 63 | + |
| 64 | +// or wait for the process to exit (with support for cancellation token) |
| 65 | +var result = await instance.WaitForExitAsync(cancellationToken); |
| 66 | +Console.WriteLine(result.ExitCode); |
| 67 | +Console.WriteLine(result.OutputData.Count); |
| 68 | +``` |
0 commit comments