Skip to content
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

Add File.ReadLinesAsync #43108

Closed
wants to merge 2 commits into from
Closed

Conversation

khellang
Copy link
Member

@khellang khellang commented Oct 6, 2020

Opening this up for early feedback. Will add XML docs and tests once I get everything set up properly 😅

Closes #2214

@Dotnet-GitSync-Bot
Copy link
Collaborator

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, to please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

@khellang khellang changed the title File.ReadLinesAsync Add File.ReadLinesAsync Oct 6, 2020
Debug.Assert(!string.IsNullOrEmpty(path));
Debug.Assert(encoding != null);

using StreamReader sr = AsyncStreamReader(path, encoding);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File.ReadLines has some special behaviors related to this. It actually creates the StreamReader as part of the ReadLines call, so that the file is opened as part of the ReadLines call and any validation as part of that (e.g. does the file exist) happens as part of the call to ReadLines, rather than as part of the first MoveNext on the resulting iterator. It then has to take care to create a new reader on subsequent iterations. For consistency between ReadLines and ReadLinesAsync, and for all the reasons ReadLines chose that approach, we should probably do the same thing here, even though it means extra complication.

cancellationToken.ThrowIfCancellationRequested();

string? line;
while ((line = await sr.ReadLineAsync().ConfigureAwait(false)) != null)
Copy link
Member

@stephentoub stephentoub Oct 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The lack of cancellation support in ReadLineAsync is unfortunate for this API in particular, as it means the cancellation token won't help unblocking work stuck because no data is available yet (e.g. if reading from a NetworkStream). We should strongly consider implementing #20824 before adding this API that would sit on top of it; otherwise, dev expectations on this ReadLinesAsync API aren't going to be met. cc: @terrajobst, @carlossanlop, @jozkee

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that would be nice. I have no problem holding off until #20824 gets in 👍🏻

@ghost ghost closed this Feb 22, 2021
@ghost
Copy link

ghost commented Feb 22, 2021

Draft Pull Request was automatically closed for inactivity. It can be manually reopened in the next 30 days if the work resumes.

@ghost ghost locked as resolved and limited conversation to collaborators Mar 24, 2021
This pull request was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

API Proposal: File.ReadLinesAsync
5 participants