-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Add File.ReadLinesAsync #43108
Conversation
Note regarding the 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. |
Debug.Assert(!string.IsNullOrEmpty(path)); | ||
Debug.Assert(encoding != null); | ||
|
||
using StreamReader sr = AsyncStreamReader(path, encoding); |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 👍🏻
9ac7d07
to
50bb919
Compare
Draft Pull Request was automatically closed for inactivity. It can be manually reopened in the next 30 days if the work resumes. |
Opening this up for early feedback. Will add XML docs and tests once I get everything set up properly 😅
Closes #2214