microsoft / dotnet

This repo is the official home of .NET on GitHub. It's a great starting point to find many .NET OSS projects from Microsoft and the community, including many that are part of the .NET Foundation.
https://devblogs.microsoft.com/dotnet/
MIT License
14.36k stars 2.22k forks source link

Can provide an public interface for "ValueTaskAwaiter" and "TaskAwaiter" ? #1441

Open AkazawaYun opened 3 months ago

AkazawaYun commented 3 months ago

can you provide an public interface for "ValueTaskAwaiter" and "TaskAwaiter" please? they are both has IsCompleted { get; } and GetResult() . it's very inconvenient to define a custom XXXAwaiter for async/await without an interface like "IAwaiter",

there is an example:

i want to add some operation when await a Task, so i add an extend method to convert a Task to my CustomTask, in the CustomTask, there is an GetAwaiter() which return a CustomAwaiter. NOW, i met some problem :

In some case, i want the CustomTask.GetAwaiter() to directly return task.GetAwaiter(), it is an "TaskAwaiter" struct, And In some other case, i want to return new CustomAwaiter(), but i can't find the return-type to adapt the two type at the same time ! because there is not an INTERFACE to the TaskAwaiter.

public interface IAwaiter : INotifyCompletion
{
    bool IsCompleted { get; }
    void GetResult();
}
public readonly struct CustomTask
{
    readonly Task task;
    readonly SemaphoreSlim? semaphore;

    public CustomTask(Task task, SemaphoreSlim? semaphore)
    {
        this.task = task;
        this.semaphore = semaphore;
    }
    public IAwaiter GetAwaiter()
    {
        //  TaskAwaiter is not implement the IAwaiter , in other words, there  is not an interface for TaskWaiter,  
        //      there is  not an interface for the CustomAwaiter to implement. 
        //      ( so I write one by myself, but it is not compatible in this situation )
        //  So, there is an compile-error.
        return semaphore is null ? task.GetAwaiter() : new CustomAwaiter(task, semaphore);
    }

    public readonly struct CustomAwaiter : IAwaiter
    {
         xxxxx 
    }
}
AkazawaYun commented 3 months ago

or there is some other way to solve it ? i need a help, thank you ! the main purpose is to limit the concurrent worker count when many tcpclients receieve new msgs at the same time , so I wait the semaphore in each msg-process-callback. To avoid someone taked too long time (ex. do some await IOMethodAsync()) , before waiting an IO operation, the semaphore need to Release(), an when it completed, the semaphore need to Wait().

AkazawaYun commented 3 months ago

well, i solve it via putting the Task's awaiter and semephore as two fields into the CustomAwaiter , to effect the behavior of the CustomAwaiter's Constructure & OnComplet() method...

Requolpev commented 2 months ago

I'll try it too.