TaskHelpers.cs 1015 B

123456789101112131415161718192021222324252627282930313233
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. #if !NO_TPL && !NO_TASK_DELAY
  5. using System.Threading;
  6. using System.Threading.Tasks;
  7. namespace System.Reactive.Concurrency
  8. {
  9. static class TaskHelpers
  10. {
  11. private const int MAX_DELAY = int.MaxValue;
  12. public static Task Delay(TimeSpan delay, CancellationToken token)
  13. {
  14. var milliseconds = (long)delay.TotalMilliseconds;
  15. if (milliseconds > MAX_DELAY)
  16. {
  17. var remainder = delay - TimeSpan.FromMilliseconds(MAX_DELAY);
  18. return
  19. Task.Delay(MAX_DELAY, token)
  20. .ContinueWith(_ => Delay(remainder, token), TaskContinuationOptions.ExecuteSynchronously)
  21. .Unwrap();
  22. }
  23. return Task.Delay(delay, token);
  24. }
  25. }
  26. }
  27. #endif