Advertisement
Guest User

Untitled

a guest
May 27th, 2015
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.55 KB | None | 0 0
  1. // I don't remember where this is taken/adapted from.
  2. public static class AsyncExtensions
  3. {
  4. public static async Task<T> TimeoutAfter<T>(this Task<T> task, TimeSpan timeout, CancellationTokenSource cancellationTokenSource = null)
  5. {
  6. if (task == await Task.WhenAny(task, Task.Delay(timeout)))
  7. return await task;
  8. else
  9. {
  10. if (cancellationTokenSource != null)
  11. cancellationTokenSource.Cancel();
  12.  
  13. throw new TimeoutException();
  14. }
  15. }
  16. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement