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

Proposal: Async Console I/O APIs #299

Open
hez2010 opened this issue Nov 26, 2019 · 4 comments
Open

Proposal: Async Console I/O APIs #299

hez2010 opened this issue Nov 26, 2019 · 4 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Console
Milestone

Comments

@hez2010
Copy link
Contributor

hez2010 commented Nov 26, 2019

Proposal

Async Console I/O APIs

Since now we can write async Task Main, I think it's necessary to add APIs to support cancellable async console I/O operations without needs of operating Console.In, Console.Out directly for converience.

Proposed APIs

public static class Console 
{
+    public static Task WriteAsync(string content, CancellationToken cancellationToken);
+    // ... other overloads for char, int and etc
+    public static Task WriteLineAsync(T content, CancellationToken cancellationToken);
+    // ... other overloads for char, int and etc
+    public static Task<int> ReadKeyAsync(CancellationToken cancellationToken);
+    public static Task<string> ReadLineAsync(CancellationToken cancellationToken);
}
@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Console untriaged New issue has not been triaged by the area owner labels Nov 26, 2019
@huoyaoyuan
Copy link
Member

There are so many overloads on textwriter. Accessing In or Out is just literally 3 or 4 characters without any code flow changes, and can be much shorter if saved as variable.

Only ReadKeyAsync is really benificial.

@fbrosseau
Copy link

Also, at least under Windows, but I would assume other implementations are similar, the in/out streams are implemented as synchronous-only streams, so the proposal to have first-class async methods on Console would be a misleading hidden threadpool block.

@adamsitnik adamsitnik added this to the Future milestone Jun 22, 2020
@adamsitnik adamsitnik added api-suggestion Early API idea and discussion, it is NOT ready for implementation and removed untriaged New issue has not been triaged by the area owner labels Jun 22, 2020
radical pushed a commit to radical/runtime that referenced this issue Jul 7, 2022
By default, we only serve files with known MIME types, but that is an issue when dotnet library tests consume over a hundred different file extensions in their tests
@adamsitnik
Copy link
Member

Console is not opened for async IO on Windows, but with #87103 and #72503 we should be able to get cancellation support on Windows. I wonder whether it would be possible to implement it on Unix.

cc @stephentoub @tmds

@tmds
Copy link
Member

tmds commented Nov 16, 2023

+    public static Task WriteAsync(string content, CancellationToken cancellationToken);
+    // ... other overloads for char, int and etc
+    public static Task WriteLineAsync(T content, CancellationToken cancellationToken);

These don't seem particularly useful: usually you want to block until the output was written.

+    public static Task<int> ReadKeyAsync(CancellationToken cancellationToken);
+    public static Task<string> ReadLineAsync(CancellationToken cancellationToken);

These have a UX race between cancellation and the user providing input: was the input for the previous or the next caller?

If ReadKeyAsync incercepts (like ReadKey(incercept: true)) on cancellation the key could go back in the queue.

I wonder whether it would be possible to implement it on Unix.

I think we may perform the read on a separate thread where it blocks until there is input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Console
Projects
None yet
Development

No branches or pull requests

6 participants