Runtime: Proposal: Async Console I/O APIs

Created on 26 Nov 2019  路  2Comments  路  Source: dotnet/runtime

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);
}
api-suggestion area-System.Console

Most helpful comment

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.

All 2 comments

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.

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.

Was this page helpful?
0 / 5 - 0 ratings