You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
}
}
The text was updated successfully, but these errors were encountered:
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
changed the title
can provide an public interface for "ValueTaskAwaiter" and "TaskAwaiter" ?
Can provide an public interface for "ValueTaskAwaiter" and "TaskAwaiter" ?
Jul 1, 2024
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...
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.
The text was updated successfully, but these errors were encountered: