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

AsyncAutoResetEvent能指定等待时间吗? #73

Open
yll690 opened this issue Mar 7, 2023 · 2 comments
Open

AsyncAutoResetEvent能指定等待时间吗? #73

yll690 opened this issue Mar 7, 2023 · 2 comments

Comments

@yll690
Copy link

yll690 commented Mar 7, 2023

如题,用Task.Wait的话又失去了async的意义

@yll690
Copy link
Author

yll690 commented Mar 8, 2023

用这种方式是否可行?
https://stackoverflow.com/a/11191070/10231865

int timeout = 1000;
var task = SomeOperationAsync(cancellationToken);
if (await Task.WhenAny(task, Task.Delay(timeout, cancellationToken)) == task)
{
    // Task completed within timeout.
    // Consider that the task may have faulted or been canceled.
    // We re-await the task so that any exceptions/cancellation is rethrown.
    await task;

}
else
{
    // timeout/cancellation logic
}

@lindexi
Copy link
Member

lindexi commented Mar 14, 2023

@yll690 可以呀

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