VirtualTimeScheduler.Extensions.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. using System.Reactive.Disposables;
  5. namespace System.Reactive.Concurrency
  6. {
  7. /// <summary>
  8. /// Provides a set of extension methods for virtual time scheduling.
  9. /// </summary>
  10. public static class VirtualTimeSchedulerExtensions
  11. {
  12. /// <summary>
  13. /// Schedules an action to be executed at dueTime.
  14. /// </summary>
  15. /// <typeparam name="TAbsolute">Absolute time representation type.</typeparam>
  16. /// <typeparam name="TRelative">Relative time representation type.</typeparam>
  17. /// <param name="scheduler">Scheduler to execute the action on.</param>
  18. /// <param name="dueTime">Relative time after which to execute the action.</param>
  19. /// <param name="action">Action to be executed.</param>
  20. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  21. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  22. public static IDisposable ScheduleRelative<TAbsolute, TRelative>(this VirtualTimeSchedulerBase<TAbsolute, TRelative> scheduler, TRelative dueTime, Action action)
  23. where TAbsolute : IComparable<TAbsolute>
  24. {
  25. if (scheduler == null)
  26. throw new ArgumentNullException(nameof(scheduler));
  27. if (action == null)
  28. throw new ArgumentNullException(nameof(action));
  29. return scheduler.ScheduleRelative(action, dueTime, Invoke);
  30. }
  31. /// <summary>
  32. /// Schedules an action to be executed at dueTime.
  33. /// </summary>
  34. /// <typeparam name="TAbsolute">Absolute time representation type.</typeparam>
  35. /// <typeparam name="TRelative">Relative time representation type.</typeparam>
  36. /// <param name="scheduler">Scheduler to execute the action on.</param>
  37. /// <param name="dueTime">Absolute time at which to execute the action.</param>
  38. /// <param name="action">Action to be executed.</param>
  39. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  40. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  41. public static IDisposable ScheduleAbsolute<TAbsolute, TRelative>(this VirtualTimeSchedulerBase<TAbsolute, TRelative> scheduler, TAbsolute dueTime, Action action)
  42. where TAbsolute : IComparable<TAbsolute>
  43. {
  44. if (scheduler == null)
  45. throw new ArgumentNullException(nameof(scheduler));
  46. if (action == null)
  47. throw new ArgumentNullException(nameof(action));
  48. return scheduler.ScheduleAbsolute(action, dueTime, Invoke);
  49. }
  50. static IDisposable Invoke(IScheduler scheduler, Action action)
  51. {
  52. action();
  53. return Disposable.Empty;
  54. }
  55. }
  56. }