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

[API Proposal]: Async overloads of Console.Write[Line] #104182

Closed
colejohnson66 opened this issue Jun 28, 2024 · 3 comments
Closed

[API Proposal]: Async overloads of Console.Write[Line] #104182

colejohnson66 opened this issue Jun 28, 2024 · 3 comments
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading

Comments

@colejohnson66
Copy link

colejohnson66 commented Jun 28, 2024

Background and motivation

Async overloads of Console.Write[Line] are not present. They should be added to avoid blocking in async methods.

API Proposal

Instead of exposing the subset of APIs on TextWriter, this contains the APIs available on Console that just forward to Out.WriteX

namespace System;

public static class Console
{
    public static Task WriteAsync(bool value);
    public static Task WriteAsync(char value);
    public static Task WriteAsync(char[]? buffer);
    public static Task WriteAsync(char[]? buffer, int index, int count);
    public static Task WriteAsync(decimal value);
    public static Task WriteAsync(double value);
    public static Task WriteAsync(float value);
    public static Task WriteAsync(int value);
    public static Task WriteAsync(long value);
    public static Task WriteAsync(object? value);
    public static Task WriteAsync(string? value);
    public static Task WriteAsync(uint value);
    public static Task WriteAsync(ulong value);

    public static Task WriteLineAsync(bool value);
    public static Task WriteLineAsync(char value);
    public static Task WriteLineAsync(char[]? buffer);
    public static Task WriteLineAsync(char[]? buffer, int index, int count);
    public static Task WriteLineAsync(decimal value);
    public static Task WriteLineAsync(double value);
    public static Task WriteLineAsync(float value);
    public static Task WriteLineAsync(int value);
    public static Task WriteLineAsync(long value);
    public static Task WriteLineAsync(object? value);
    public static Task WriteLineAsync(string? value);
    public static Task WriteLineAsync(uint value);
    public static Task WriteLineAsync(ulong value);

    // these ones could probably be tossed in favor of interpolated strings decaying to `string`
    public static Task WriteAsync([StringSyntax("CompositeFormat")] string format, object? arg0);
    public static Task WriteAsync([StringSyntax("CompositeFormat")] string format, object? arg0, object? arg1);
    public static Task WriteAsync([StringSyntax("CompositeFormat")] string format, object? arg0, object? arg1, object? arg2);
    public static Task WriteAsync([StringSyntax("CompositeFormat")] string format, object? arg0, object? arg1, object? arg2, object? arg3);
    public static Task WriteAsync([StringSyntax("CompositeFormat")] string format, params object?[] args);
    public static Task WriteLineAsync([StringSyntax("CompositeFormat")] string format, object? arg0);
    public static Task WriteLineAsync([StringSyntax("CompositeFormat")] string format, object? arg0, object? arg1);
    public static Task WriteLineAsync([StringSyntax("CompositeFormat")] string format, object? arg0, object? arg1, object? arg2);
    public static Task WriteLineAsync([StringSyntax("CompositeFormat")] string format, object? arg0, object? arg1, object? arg2, object? arg3);
    public static Task WriteLineAsync([StringSyntax("CompositeFormat")] string format, params object?[] args);
}

API Usage

Essentially, this would prevent blocking of work while waiting for the console output to complete.

Task? t = null;
while (NeedToDoWork())
{
    if (t is not null)
        await t; // wait for previous write to finish
    t = Console.WriteLineAsync("Starting next task.");
    DoWork();
}

Alternative Designs

I've opted to duplicate the current API surface of Console. A more limited alternative would be one where only the async methods present in TextWriter are exposed.

Alternatively, an analyzer that detects usage of Console.Write[Line] in asynchronous methods could suggest rewriting it to Console.Out.Write[Line]Async. However, the API surface of Console is different from that of TextWriter - only char/string-based overloads are present there.

Risks

Existing analyzers would now detect Console.Write[Line](...) in async methods and suggest rewriting to await Console.Write[Line]Async(...).

@colejohnson66 colejohnson66 added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Jun 28, 2024
@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Jun 28, 2024
Copy link
Contributor

Tagging subscribers to this area: @mangod9
See info in area-owners.md if you want to be subscribed.

@colejohnson66 colejohnson66 changed the title [API Proposal]: Expose async Console.Out.WriteXAsync in Console [API Proposal]: Create async overloads of Console.Write[Line] Jun 28, 2024
@colejohnson66 colejohnson66 changed the title [API Proposal]: Create async overloads of Console.Write[Line] [API Proposal]: Async overloads of Console.Write[Line] Jun 28, 2024
@alexrp
Copy link
Contributor

alexrp commented Jun 29, 2024

Related: #299

@stephentoub
Copy link
Member

Duplicate of #299. Thanks.

@stephentoub stephentoub closed this as not planned Won't fix, can't repro, duplicate, stale Jun 29, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Jun 29, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jul 29, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation area-System.Threading
Projects
None yet
Development

No branches or pull requests

3 participants