-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Add CancellationToken to TextWriter.FlushAsync #64340
Comments
Tagging subscribers to this area: @dotnet/area-system-io Issue DetailsBackground and motivationSome Some implementations also buffer data before reaching the underlying stream, and without a call to Calling With the merging of #61898, both Note that the References:
API Proposalpublic abstract class TextWriter : MarshalByRefObject, IAsyncDisposable, IDisposable
{
public virtual System.Threading.Tasks.Task FlushAsync (CancellationToken cancellationToken);
} API Usageasync Task PrintReportAsync(TextWriter writer, CancellationToken cancellationToken = default)
{
await writer.WriteLineAsync("Some data".AsMemory(), cancellationToken);
await writer.FlushAsync(cancellationToken);
} Alternative DesignsAlternatively, public abstract class TextWriter : MarshalByRefObject, IAsyncDisposable, IDisposable
{
public virtual System.Threading.Tasks.ValueTask FlushAsync (CancellationToken cancellationToken);
} RisksNo response
|
namespace System.IO;
public partial class TextWriter
{
// Existing:
// public virtual Task FlushAsync();
public virtual Task FlushAsync(CancellationToken cancellationToken);
} |
Background and motivation
Some
TextWriter
implementations such asStreamWriter
serve to encode data in a particular format and then write that data to an underlying data stream. The underlying data stream could easily be a file stream or network stream, where it is desired to be able to cancel any pending operation upon either an upstream cancellation (e.g. termination of the HTTP request that initiated such as data transfer) or user request (e.g. user clicks Cancel button).Some implementations also buffer data before reaching the underlying stream, and without a call to
Flush
(orAutoFlush = true
), do not flush those buffers to the underlying stream. So it is sometimes necessary in these instances to callFlush
to ensure that theTextWriter
implementation has flushed its buffers to the underlying stream, even if it is not necessary to flush the underlying stream. Typical implementations ofFlush
however will typically flush their own buffers and then call theFlush
method of the underlying stream.Calling
FlushAsync
on an underlying data stream can often result in an asynchronous delay while the data is written to disk or sent over the network. However, the method does not provide any means to cancel the underlying write/flush operation.With the merging of #61898, both
TextReader
andTextWriter
supports cancellation tokens for each of their methods (in some form) except this one.Note that the
Stream.FlushAsync
method has an overload with a cancellation token since .NET Standard 1.0, so there is underlying support for the means to asynchronously flush buffers to disk/network as described in the example above.API Proposal
API Usage
Alternative Designs
Alternatively,
ValueTask
could be used, but based on the other APIs that do not return a value, it would seem thatTask
is preferred here.Risks
No response
References
The text was updated successfully, but these errors were encountered: