1
0

Scheduler.Simple.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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;
  5. using System.Reactive.Disposables;
  6. namespace System.Reactive.Concurrency
  7. {
  8. public static partial class Scheduler
  9. {
  10. /// <summary>
  11. /// Schedules an action to be executed.
  12. /// </summary>
  13. /// <param name="scheduler">Scheduler to execute the action on.</param>
  14. /// <param name="action">Action to execute.</param>
  15. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  16. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  17. public static IDisposable Schedule(this IScheduler scheduler, Action action)
  18. {
  19. if (scheduler == null)
  20. throw new ArgumentNullException("scheduler");
  21. if (action == null)
  22. throw new ArgumentNullException("action");
  23. return scheduler.Schedule(action, Invoke);
  24. }
  25. /// <summary>
  26. /// Schedules an action to be executed after the specified relative due time.
  27. /// </summary>
  28. /// <param name="scheduler">Scheduler to execute the action on.</param>
  29. /// <param name="action">Action to execute.</param>
  30. /// <param name="dueTime">Relative time after which to execute the action.</param>
  31. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  32. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  33. public static IDisposable Schedule(this IScheduler scheduler, TimeSpan dueTime, Action action)
  34. {
  35. if (scheduler == null)
  36. throw new ArgumentNullException("scheduler");
  37. if (action == null)
  38. throw new ArgumentNullException("action");
  39. return scheduler.Schedule(action, dueTime, Invoke);
  40. }
  41. /// <summary>
  42. /// Schedules an action to be executed at the specified absolute due time.
  43. /// </summary>
  44. /// <param name="scheduler">Scheduler to execute the action on.</param>
  45. /// <param name="action">Action to execute.</param>
  46. /// <param name="dueTime">Absolute time at which to execute the action.</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 null.</exception>
  49. public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset dueTime, Action action)
  50. {
  51. if (scheduler == null)
  52. throw new ArgumentNullException("scheduler");
  53. if (action == null)
  54. throw new ArgumentNullException("action");
  55. return scheduler.Schedule(action, dueTime, Invoke);
  56. }
  57. /// <summary>
  58. /// Schedules an action to be executed.
  59. /// </summary>
  60. /// <param name="scheduler">Scheduler to execute the action on.</param>
  61. /// <param name="action">Action to execute.</param>
  62. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  63. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is null.</exception>
  64. public static IDisposable ScheduleLongRunning(this ISchedulerLongRunning scheduler, Action<ICancelable> action)
  65. {
  66. if (scheduler == null)
  67. throw new ArgumentNullException("scheduler");
  68. if (action == null)
  69. throw new ArgumentNullException("action");
  70. return scheduler.ScheduleLongRunning(action, (a, c) => a(c));
  71. }
  72. static IDisposable Invoke(IScheduler scheduler, Action action)
  73. {
  74. action();
  75. return Disposable.Empty;
  76. }
  77. }
  78. }