You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[API Proposal]: Process.RunAndCaptureLinesAsync for reading output lines streaming #128280
wondering that maybe we could also add static methods Process.RunAndCaptureLinesAsync likes Process.RunAndCaptureTextAsync to simplify the read process output lines as a stream
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?
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.
In #123959 I've initially included following proposal:
publicsealedclassProcessOutputLines:IAsyncEnumerable<ProcessOutputLine>,IEnumerable<ProcessOutputLine>{publicintProcessId{get;}// Available after enumeration startspublicProcessExitStatusExitStatus{get;}// Available after enumeration completespublicasyncIAsyncEnumerator<ProcessOutputLine>GetAsyncEnumerator(CancellationTokencancellationToken=default);publicIEnumerator<ProcessOutputLine>GetEnumerator();IEnumeratorIEnumerable.GetEnumerator();}
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:
namespaceSystem.Diagnostics;publicsealedclassProcessLinesOutput// to be consistent with ProcessTextOutput name{publicintProcessId{get;}publicProcessExitStatusExitStatus{get;}// exposing two properties to avoid Lines.Any(line => line.StandardError)publicIReadOnlyList<ProcessOutputLine>StandardOutputLines{get;}publicIReadOnlyList<ProcessOutputLine>StandardErrorLines{get;}}publicclassProcess{publicstaticProcessLinesOutputRunAndCaptureLines(stringfileName,IList<string>?arguments=null,TimeSpan?timeout=default);publicstaticProcessLinesOutputRunAndCaptureLines(ProcessStartInfostartInfo,TimeSpan?timeout=default);publicstaticTask<ProcessLinesOutput>RunAndCaptureLinesAsync(stringfileName,IList<string>?arguments=null,CancellationTokencancellationToken=default);publicstaticTask<ProcessLinesOutput>RunAndCaptureLinesAsync(ProcessStartInfostartInfo,CancellationTokencancellationToken=default);}
namespaceSystem.Diagnostics;publicsealedclassProcessLinesOutput// to be consistent with ProcessTextOutput name{publicintProcessId{get;}publicProcessExitStatusExitStatus{get;}// exposing one property to preserve orderingpublicIReadOnlyList<ProcessOutputLine>Lines{get;}}publicclassProcess{publicstaticProcessLinesOutputRunAndCaptureLines(stringfileName,IList<string>?arguments=null,TimeSpan?timeout=default);publicstaticProcessLinesOutputRunAndCaptureLines(ProcessStartInfostartInfo,TimeSpan?timeout=default);publicstaticTask<ProcessLinesOutput>RunAndCaptureLinesAsync(stringfileName,IList<string>?arguments=null,CancellationTokencancellationToken=default);publicstaticTask<ProcessLinesOutput>RunAndCaptureLinesAsync(ProcessStartInfostartInfo,CancellationTokencancellationToken=default);}
@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
Activity
dotnet-policy-service commentedon May 16, 2026
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.
tmds commentedon May 18, 2026
The return types of the
static Runmethods onProcessall include the exit status.With the instance
ReadAllLinesAsync, you can get the exit status from the process instance.WeihanLi commentedon May 19, 2026
What about introducing a delegate when the process exits?
tmds commentedon May 19, 2026
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.
adamsitnik commentedon May 19, 2026
In #123959 I've initially included following proposal:
We could combine it with:
Sample usage:
But in contrary to
ReadAllLineswe don't have the ability to specifyTimeSpan timeoutfor the sync overload. And I am not sure if users would realize thatStreamOutputLines(noAsyncsuffix) 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:We could use it in places like here microsoft/aspire#17079 (comment) where the output is read and then split into lines.
tmds commentedon May 19, 2026
Personally I prefer this as the instance method because unlike the other
Runmethods,IAsyncEnumerable<ProcessOutputLine>represents a live process.This leaks a child.
adamsitnik commentedon May 21, 2026
Great point @tmds
Which leaves us with:
@WeihanLi would that meet your requirements?
WeihanLi commentedon May 26, 2026
@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
IAsyncEnumerablereturn, think I could use the instance methodReadAllLinesAsyncfor now, feel free to close this oneadamsitnik commentedon May 26, 2026
@WeihanLi @tmds Thank you both for your feedback and great discussion!