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

Yoshifumi Kawai
2 min readOct 26, 2020

The only way to handle external processes in C# is with the Process class, but this API is very old, and asynchronous support would require a lot of flag and boilerplate code.

So I created a library that “just throws a line of string like writing a shell,” “receives the result in a C# 8.0 asynchronous stream, which also only requires a line of await foreach,” and “handles ExitCode, StdError, etc. appropriately.

There are other wrapper libraries for Process. However, assuming C# 8.0 Async Streams, ProcessX makes it the simplest.

What would normally take about 30 lines to write in Process, only takes one line.

Process’s design has not changed in any way since the .NET Framework 1.0, and it is clearly outdated. It’s very difficult to use, even though it hides a half-baked, low-level process. It’s a mess, especially asynchronous processing.

Now, the nice thing about await foreach is that you can use try-catch for exception handling as is, so if ExitCode is non-zero (or if you receive a StdError), ProcessErrorException will be raised.

If you want to wait for synchronization and get all the results, like WaitForExit, you can use ToTask.

As for cancellation, you can still use WithCancellation for asynchronous streams. If the process is still intact when canceling, kill it with the cancellation to ensure it’s killed.

The timeout can be used because the CancelationTokenSource itself has the option of firing with time.

We also have a ProcessX.StartAsync overload that allows you to set the working directory, environment variables, and encoding, so you should be able to implement most things without problems.

Process itself is the C# 1.0 generation (10 years ago!).
It’s worthwhile to provide a properly designed modern library again. Both the design techniques and the language itself are far more advanced, so a proper update is necessary.

We offer a library called ConsoleAppFramework, which is also a modern interpretation of CLI tools/command line parsing. We will be the ones to evolve C# in the .NET Core era.

--

--

Yoshifumi Kawai

a.k.a. neuecc. Creator of UniRx, UniTask, MessagePack for C#, MagicOnion etc. Microsoft MVP for C#. CEO/CTO of Cysharp Inc. Live and work in Tokyo, Japan.