1
0

VirtualTimeScheduler.Extensions.cs 3.5 KB

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