Skip to content

[API Proposal]: Process.RunAndCaptureLinesAsync for reading output lines streaming #128280

Closed as not planned

Description

@WeihanLi

Background and motivation

Since we've introduced some new APIs for Process

wondering that maybe we could also add static methods Process.RunAndCaptureLinesAsync likes Process.RunAndCaptureTextAsync to simplify the read process output lines as a stream

API Proposal

namespace System.Diagnostics;

public class Process
{
    public static IAsyncEnumerable<ProcessOutputLine> RunAndCaptureLinesAsync(string fileName, IList<string>? arguments = null, CancellationToken cancellationToken = default);
    public static IAsyncEnumerable<ProcessOutputLine> RunAndCaptureLinesAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default);
}

API Usage

await foreach (var line in Process.RunAndCaptureLinesAsync("dotnet", ["--info"]))
{
    ConsoleHelper.WriteLineWithColor(line.Content, line.StandardError ? ConsoleColor.Red : ConsoleColor.Green);
}

Alternative Designs

No response

Risks

No response

Activity

  1. added
    api-suggestion
    on May 16, 2026
  2. added
    untriaged
    on May 16, 2026
  3. dotnet-policy-service commented on May 16, 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.

  4. tmds commented on May 18, 2026

    @tmds
    Member

    The return types of the static Run methods on Process all include the exit status.

    With the instance ReadAllLinesAsync, you can get the exit status from the process instance.

  5. WeihanLi commented on May 19, 2026

    @WeihanLi
    ContributorAuthor

    The return types of the static Run methods on Process all include the exit status.
    With the instance ReadAllLinesAsync, you can get the exit status from the process instance.

    What about introducing a delegate when the process exits?

    public static IAsyncEnumerable<ProcessOutputLine> RunAndCaptureLinesAsync(ProcessStartInfo startInfo, Func<int, Task>? onExit = null, CancellationToken cancellationToken = default);
  6. tmds commented on May 19, 2026

    @tmds
    Member

    There are no other APIs with such a shape. The event/callback APIs is what the new Process APIs are avoiding in favor of async APIs. Also, it doesn't encourage users to check the exit code.

  7. adamsitnik commented on May 19, 2026

    @adamsitnik
    Member

    In #123959 I've initially included following proposal:

    public sealed class ProcessOutputLines : IAsyncEnumerable<ProcessOutputLine>, IEnumerable<ProcessOutputLine>
    {
        public int ProcessId { get; } // Available after enumeration starts
        public ProcessExitStatus ExitStatus { get; } // Available after enumeration completes
    
        public async IAsyncEnumerator<ProcessOutputLine> GetAsyncEnumerator(CancellationToken cancellationToken = default);
        public IEnumerator<ProcessOutputLine> GetEnumerator();
        IEnumerator IEnumerable.GetEnumerator();
    }

    We could combine it with:

    public class Process
    {
        public static ProcessOutputLines StreamOutputLines(string fileName, IList<string>? arguments = null);
        public static ProcessOutputLines StreamOutputLines(ProcessStartInfo startInfo);
    }

    Sample usage:

    await foreach (ProcessOutputLine line in Process.StreamOutputLines("dotnet", ["--help]))
    {
        if (line.StandardError)
        {
            Console.Error.WriteLine($"ERR: {line.Content}");
        }
        else
        {
            Console.WriteLine($"OUT: {line.Content}");
        }
    }

    But in contrary to ReadAllLines we don't have the ability to specify TimeSpan timeout for the sync overload. And I am not sure if users would realize that StreamOutputLines (no Async suffix) allows you to perform both sync and async streaming.

    An alternative to streaming (already offered by the Process.ReadAllLines[Async] methods), we could provide a method that returns when all lines have been read:

    namespace System.Diagnostics;
    
    public sealed class ProcessLinesOutput // to be consistent with ProcessTextOutput name
    {
        public int ProcessId { get; }
        public ProcessExitStatus ExitStatus { get; }
    
        // exposing two properties to avoid Lines.Any(line => line.StandardError)
        public IReadOnlyList<ProcessOutputLine> StandardOutputLines { get; }
        public IReadOnlyList<ProcessOutputLine> StandardErrorLines { get; }
    }
    
    public class Process
    {
        public static ProcessLinesOutput RunAndCaptureLines(string fileName, IList<string>? arguments = null, TimeSpan? timeout = default);
        public static ProcessLinesOutput RunAndCaptureLines(ProcessStartInfo startInfo, TimeSpan? timeout = default);
        public static Task<ProcessLinesOutput> RunAndCaptureLinesAsync(string fileName, IList<string>? arguments = null, CancellationToken cancellationToken = default);
        public static Task<ProcessLinesOutput> RunAndCaptureLinesAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default);
    }

    We could use it in places like here microsoft/aspire#17079 (comment) where the output is read and then split into lines.

  8. removed
    untriaged
    on May 19, 2026
  9. added this to the 11.0.0 milestone on May 19, 2026
  10. tmds commented on May 19, 2026

    @tmds
    Member

    Personally I prefer this as the instance method because unlike the other Run methods, IAsyncEnumerable<ProcessOutputLine> represents a live process.

    string version = "";
    await foreach (ProcessOutputLine line in Process.RunAndCaptureLines("dotnet", ["--info"]))
    {
        if (line.Content.Contains("Version"))
        {
            version = line.Content.Split(':')[1].Trim();
            break;
        }
    }

    This leaks a child.

  11. adamsitnik commented on May 21, 2026

    @adamsitnik
    Member

    This leaks a child.

    Great point @tmds

    Which leaves us with:

    namespace System.Diagnostics;
    
    public sealed class ProcessLinesOutput // to be consistent with ProcessTextOutput name
    {
        public int ProcessId { get; }
        public ProcessExitStatus ExitStatus { get; }
    
        // exposing one property to preserve ordering
        public IReadOnlyList<ProcessOutputLine> Lines { get; }
    }
    
    public class Process
    {
        public static ProcessLinesOutput RunAndCaptureLines(string fileName, IList<string>? arguments = null, TimeSpan? timeout = default);
        public static ProcessLinesOutput RunAndCaptureLines(ProcessStartInfo startInfo, TimeSpan? timeout = default);
        public static Task<ProcessLinesOutput> RunAndCaptureLinesAsync(string fileName, IList<string>? arguments = null, CancellationToken cancellationToken = default);
        public static Task<ProcessLinesOutput> RunAndCaptureLinesAsync(ProcessStartInfo startInfo, CancellationToken cancellationToken = default);
    }

    @WeihanLi would that meet your requirements?

  12. WeihanLi commented on May 26, 2026

    @WeihanLi
    ContributorAuthor

    @adamsitnik thanks for the thoughts, mainly want to stream the output to avoid waiting for the process exits, since sometimes the process may cost a lot of time, streaming output would help user get feedback in time to improve the user experience.
    The public IReadOnlyList<ProcessOutputLine> Lines { get; } seems only available when the process exited?
    Considering the concerns @tmds raised about the static method with IAsyncEnumerable return, think I could use the instance method ReadAllLinesAsync for now, feel free to close this one

  13. adamsitnik commented on May 26, 2026

    @adamsitnik
    Member

    @WeihanLi @tmds Thank you both for your feedback and great discussion!

  14. locked and limited conversation to collaborators on Jun 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionarea-System.Diagnostics.Process

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions