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

Setting AsyncAutoResetEvent after used in Task.WhenAny #283

Closed
kakkamstrup opened this issue Nov 18, 2023 · 3 comments
Closed

Setting AsyncAutoResetEvent after used in Task.WhenAny #283

kakkamstrup opened this issue Nov 18, 2023 · 3 comments

Comments

@kakkamstrup
Copy link

    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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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

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