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

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

Open
AkazawaYun opened this issue Jul 1, 2024 · 3 comments

Comments

@AkazawaYun
Copy link

AkazawaYun commented Jul 1, 2024

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

@AkazawaYun AkazawaYun changed the title can provide an public interface for "ValueTaskAwaiter" and "TaskAwaiter" ? Can provide an public interface for "ValueTaskAwaiter" and "TaskAwaiter" ? Jul 1, 2024
@AkazawaYun
Copy link
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...

@Requolpev
Copy link

I'll try it too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants