Scheduler.Simple.cs 4.0 KB

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