AsyncWorkerCollection icon indicating copy to clipboard operation
AsyncWorkerCollection copied to clipboard

AsyncAutoResetEvent能指定等待时间吗?

Open yll690 opened this issue 2 years ago • 2 comments

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

yll690 avatar Mar 07 '23 02:03 yll690

用这种方式是否可行? 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
}

yll690 avatar Mar 08 '23 05:03 yll690

@yll690 可以呀

lindexi avatar Mar 14 '23 01:03 lindexi