Skip to content

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

Open

Description

@AkazawaYun

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 
    }
}

Activity

  1. AkazawaYun commented on Jul 1, 2024

    @AkazawaYun
    Author

    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().

  2. changed the title [-]can provide an public interface for "ValueTaskAwaiter" and "TaskAwaiter" ?[/-] [+]Can provide an public interface for "ValueTaskAwaiter" and "TaskAwaiter" ?[/+] on Jul 1, 2024
  3. AkazawaYun commented on Jul 3, 2024

    @AkazawaYun
    Author

    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...

  4. Requolpev commented on Jul 11, 2024

    @Requolpev

    I'll try it too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions