VirtualTimeScheduler.Extensions.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 <paramref name="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 <c>null</c>.</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. // As stated in Scheduler.Simple.cs,
  30. // an anonymous delegate will allow delegate caching.
  31. // Watch https://github.com/dotnet/roslyn/issues/5835 for compiler
  32. // support for caching delegates from method groups.
  33. return scheduler.ScheduleRelative(action, dueTime, (s, a) => Invoke(s, a));
  34. }
  35. /// <summary>
  36. /// Schedules an action to be executed at <paramref name="dueTime"/>.
  37. /// </summary>
  38. /// <typeparam name="TAbsolute">Absolute time representation type.</typeparam>
  39. /// <typeparam name="TRelative">Relative time representation type.</typeparam>
  40. /// <param name="scheduler">Scheduler to execute the action on.</param>
  41. /// <param name="dueTime">Absolute time at which to execute the action.</param>
  42. /// <param name="action">Action to be executed.</param>
  43. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  44. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  45. public static IDisposable ScheduleAbsolute<TAbsolute, TRelative>(this VirtualTimeSchedulerBase<TAbsolute, TRelative> scheduler, TAbsolute dueTime, Action action)
  46. where TAbsolute : IComparable<TAbsolute>
  47. {
  48. if (scheduler == null)
  49. throw new ArgumentNullException(nameof(scheduler));
  50. if (action == null)
  51. throw new ArgumentNullException(nameof(action));
  52. return scheduler.ScheduleAbsolute(action, dueTime, (s, a) => Invoke(s, a));
  53. }
  54. private static IDisposable Invoke(IScheduler scheduler, Action action)
  55. {
  56. action();
  57. return Disposable.Empty;
  58. }
  59. }
  60. }