Skip to content

[API Proposal]: One-liner for running process with standard handles redirected to null device #128453

Closed

Description

@adamsitnik

Background and motivation

We have recently introduced two static Process methods: Run and RunAndCaptureText. Both start the process, wait for exit and return exit status. Run inherits standard handles from the parent, while RunAndCaptureText redirects to pipes and captures both std out and err by reding from these pipes. A very common .NET anti-pattern (example: https://github.com/microsoft/aspire/pull/17079/changes#r3243682797) is to redirect both std out and err, start the process and wait for its exit without capturing the output and error just to ensure that nothing gets printed into terminal. This can lead into deadlocks (output and error are not being drained) like it did in dotnet/aspnetcore#65518.

With the new APIs, the users can open the null file handle, configure it via Standard[Input/Output/Error]Handle properties and just pass such ProcessStartInfo to run:

using SafeFileHandle nullHandle = File.OpenNullHandle();
ProcessStartInfo psi = new("exeName", ["--args"])
{
    StandardInputHandle = nullHandle,
    StandardOutputHandle = nullHandle,
    StandardErrorHandle= nullHandle
};

Process.Run(psi);

Since this anti-pattern is so common, we could introduce a helper method that redirects standard handles to null file and waits for the process to exit. One possible name isProcess.RunWithoutOutput, another could be Process.RunAndDiscardOutput.

API Proposal

namespace System.Diagnostics
{
    public class Process : System.ComponentModel.Component, System.IDisposable
    {
        public static ProcessExitStatus RunAndDiscardOutput(ProcessStartInfo startInfo, TimeSpan? timeout = default);
        public static ProcessExitStatus RunAndDiscardOutput(string fileName, IList<string>? arguments = null, TimeSpan? timeout = default);
        public static Task<ProcessExitStatus> RunAndDiscardOutputAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default);
        public static Task<ProcessExitStatus> RunAndDiscardOutputAsync(string fileName, IList<string>? arguments = null, CancellationToken cancellationToken = default);
    }
}

API Usage

ProcessExitStatus exitStatus = Process.RunAndDiscardOutput("dotnet", ["restore"]);
if (exitStatus.ExitCode != 0)
{
    // Handle error
}

Alternative Designs

An alternative would be to extend the Run method with a boolean parameter called silent or similar.

namespace System.Diagnostics
{
    public class Process : System.ComponentModel.Component, System.IDisposable
    {
-       public static ProcessExitStatus Run(ProcessStartInfo startInfo, TimeSpan? timeout = default);
+       public static ProcessExitStatus Run(ProcessStartInfo startInfo, bool silent = false, TimeSpan? timeout = default);
-       public static ProcessExitStatus Run(string fileName, IList<string>? arguments = null, TimeSpan? timeout = default);
+       public static ProcessExitStatus Run(string fileName, IList<string>? arguments = null, bool silent = false, TimeSpan? timeout = default);
-       public static Task<ProcessExitStatus> RunAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default);
+       public static Task<ProcessExitStatus> RunAsync(ProcessStartInfo startInfo, bool silent = false, CancellationToken cancellationToken = default);
-       public static Task<ProcessExitStatus> RunAsync(string fileName, IList<string>? arguments = null, CancellationToken cancellationToken = default);
+       public static Task<ProcessExitStatus> RunAsync(string fileName, IList<string>? arguments = null, bool silent = false, CancellationToken cancellationToken = default);
    }
}

When set to true, it would redirect all standard handles to null.

Risks

No response

Activity

  1. added this to the 11.0.0 milestone on May 21, 2026
  2. self-assigned this
    on May 21, 2026
  3. added
    api-ready-for-review
    on May 21, 2026
  4. dotnet-policy-service commented on May 21, 2026

    @dotnet-policy-service
    Contributor

    Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
    See info in area-owners.md if you want to be subscribed.

  5. adamsitnik commented on May 21, 2026

    @adamsitnik
    MemberAuthor

    cc @tmds @jkotas I've marked it as ready for review to book a slot in the Design queue, I am open to modifications based on feedback.

    cc @Youssef1313 @eerhardt this method could be used in dotnet/aspnetcore#66727 and microsoft/aspire#17079, please share your feedback

  6. Youssef1313 commented on May 21, 2026

    @Youssef1313
    Member

    I would love to see that landing in .NET 11 for sure :)
    That's going to help further reduce the boilerplate and make the implementation more efficient (today the "obvious" API for this would be still reading the output but throwing it away - unless people really know to create the null handle). Given this is likely to be a common case, it makes sense to have a oneliner for it, aligning with the other oneliners that represent common cases.

  7. bartonjs commented on Jun 16, 2026

    @bartonjs
    Member

    Video

    • ProcessStartInfo can already handle this situation, but we can do it with optional parameters to the Run methods that take a fileName.
    • We discussed alternatives for "silent" like "discardOutputs", but kept it as "silent"
    • The "removed" methods from this proposal are new in .NET 11 previews, so they are being modified before release rather than just being stripped of their defaults.
      • That makes this a preview->preview breaking change.
    namespace System.Diagnostics
    {
        public class Process : System.ComponentModel.Component, System.IDisposable
        {
    -       public static ProcessExitStatus Run(string fileName, IList<string>? arguments = null, TimeSpan? timeout = default);
    +       public static ProcessExitStatus Run(string fileName, IList<string>? arguments = null, bool silent = false, TimeSpan? timeout = default);
    -       public static Task<ProcessExitStatus> RunAsync(string fileName, IList<string>? arguments = null, CancellationToken cancellationToken = default);
    +       public static Task<ProcessExitStatus> RunAsync(string fileName, IList<string>? arguments = null, bool silent = false, CancellationToken cancellationToken = default);
        }
    }
  8. added
    api-approved
    breaking-change
    and removed
    api-ready-for-review
    on Jun 16, 2026
  9. added
    needs-breaking-change-doc-created
    on Jun 16, 2026
  10. dotnet-policy-service commented on Jun 16, 2026

    @dotnet-policy-service
    Contributor

    Added needs-breaking-change-doc-created label because this issue has the breaking-change label.

    1. Create and link to this issue a matching issue in the dotnet/docs repo using the breaking change documentation template, then remove this needs-breaking-change-doc-created label.

    Tagging @dotnet/compat for awareness of the breaking change.

  11. added a commit that references this issue on Jun 17, 2026
    39a496d
  12. added a commit that references this issue on Jul 15, 2026
    add321e
  13. locked and limited conversation to collaborators on Jul 18, 2026
  14. added a commit that references this issue on Jul 22, 2026
    405b570
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

api-approvedarea-System.Diagnostics.Processbreaking-changeneeds-breaking-change-doc-created

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions