StephenCleary / AsyncEx

A helper library for async/await.
MIT License
3.49k stars 358 forks source link

Setting AsyncAutoResetEvent after used in Task.WhenAny #283

Closed kakkamstrup closed 9 months ago

kakkamstrup commented 9 months ago
    AsyncAutoResetEvent runEvent = new();
    await Task.WhenAny(runEvent.WaitAsync(), Task.Delay(TimeSpan.FromSeconds(1)));
    runEvent.Set(); // This doesn't set runEvent.IsSet to true and the following line doesn't return immediately
    await Task.WhenAny(runEvent.WaitAsync(), Task.Delay(TimeSpan.FromSeconds(5)));
StephenCleary commented 9 months ago

This is how auto-reset events work: when they become set, they release all waiters and immediately become unset again. Did you perhaps want to use a manual-reset event?

kakkamstrup commented 9 months ago

I think I know how auto-reset events work, but I guess I don't know Task.WhenAny. When the task from WhenAny completes because of Task.Delay, the runEvent.WaitAsync()-task is alive, but "abandoned" and it's the abandoned task that gets signaled. I think I get it now. Thank you for your response.

StephenCleary commented 9 months ago

Ah. Yes, your description is correct: the task previously returned from WaitAsync is completed (but is ignored by the calling code).