TaskExt.cs 553 B

1234567891011121314151617
  1. namespace qdr.fnd.core.pqueue.fnd.candidates
  2. {
  3. internal static class TaskExt
  4. {
  5. public static async Task TimeoutAfter(this Task task,CancellationTokenSource timeoutCancellationTokenSource, TimeSpan timeout)
  6. {
  7. if (await Task.WhenAny(task, Task.Delay(timeout, timeoutCancellationTokenSource.Token)) == task)
  8. {
  9. timeoutCancellationTokenSource.Cancel();
  10. await task;
  11. return;
  12. }
  13. throw new TimeoutException("The operation has timed out.");
  14. }
  15. }
  16. }