Scheduler.Simple.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. public static partial class Scheduler
  8. {
  9. /// <summary>
  10. /// Schedules an action to be executed.
  11. /// </summary>
  12. /// <param name="scheduler">Scheduler to execute the action on.</param>
  13. /// <param name="action">Action to execute.</param>
  14. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  15. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  16. public static IDisposable Schedule(this IScheduler scheduler, Action action)
  17. {
  18. if (scheduler == null)
  19. {
  20. throw new ArgumentNullException(nameof(scheduler));
  21. }
  22. if (action == null)
  23. {
  24. throw new ArgumentNullException(nameof(action));
  25. }
  26. // Surprisingly, passing the method group of Invoke will create a fresh
  27. // delegate each time although it's static, while an anonymous
  28. // lambda without the need of a closure will be cached.
  29. // Once Roslyn supports caching delegates for method groups,
  30. // the anonymous lambda can be replaced by the method group again. Until then,
  31. // to avoid the repetition of code, the call to Invoke is left intact.
  32. // Watch https://github.com/dotnet/roslyn/issues/5835
  33. return scheduler.Schedule(action, static (s, a) => Invoke(s, a));
  34. }
  35. /// <summary>
  36. /// Schedules an action to be executed.
  37. /// </summary>
  38. /// <param name="scheduler">Scheduler to execute the action on.</param>
  39. /// <param name="state">A state object to be passed to <paramref name="action"/>.</param>
  40. /// <param name="action">Action to execute.</param>
  41. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  42. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  43. // Note: The naming of that method differs because otherwise, the signature would cause ambiguities.
  44. internal static IDisposable ScheduleAction<TState>(this IScheduler scheduler, TState state, Action<TState> action)
  45. {
  46. if (scheduler == null)
  47. {
  48. throw new ArgumentNullException(nameof(scheduler));
  49. }
  50. if (action == null)
  51. {
  52. throw new ArgumentNullException(nameof(action));
  53. }
  54. return scheduler.Schedule(
  55. (action, state),
  56. (_, tuple) =>
  57. {
  58. tuple.action(tuple.state);
  59. return Disposable.Empty;
  60. });
  61. }
  62. /// <summary>
  63. /// Schedules an action to be executed.
  64. /// </summary>
  65. /// <param name="scheduler">Scheduler to execute the action on.</param>
  66. /// <param name="state">A state object to be passed to <paramref name="action"/>.</param>
  67. /// <param name="action">Action to execute.</param>
  68. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  69. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  70. // Note: The naming of that method differs because otherwise, the signature would cause ambiguities.
  71. internal static IDisposable ScheduleAction<TState>(this IScheduler scheduler, TState state, Func<TState, IDisposable> action)
  72. {
  73. if (scheduler == null)
  74. throw new ArgumentNullException(nameof(scheduler));
  75. if (action == null)
  76. throw new ArgumentNullException(nameof(action));
  77. return scheduler.Schedule(
  78. (action, state),
  79. static (_, tuple) => tuple.action(tuple.state));
  80. }
  81. /// <summary>
  82. /// Schedules an action to be executed after the specified relative due time.
  83. /// </summary>
  84. /// <param name="scheduler">Scheduler to execute the action on.</param>
  85. /// <param name="action">Action to execute.</param>
  86. /// <param name="dueTime">Relative time after which to execute the action.</param>
  87. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  88. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  89. public static IDisposable Schedule(this IScheduler scheduler, TimeSpan dueTime, Action action)
  90. {
  91. if (scheduler == null)
  92. {
  93. throw new ArgumentNullException(nameof(scheduler));
  94. }
  95. if (action == null)
  96. {
  97. throw new ArgumentNullException(nameof(action));
  98. }
  99. // See note above.
  100. return scheduler.Schedule(action, dueTime, static (s, a) => Invoke(s, a));
  101. }
  102. /// <summary>
  103. /// Schedules an action to be executed after the specified relative due time.
  104. /// </summary>
  105. /// <param name="scheduler">Scheduler to execute the action on.</param>
  106. /// <param name="action">Action to execute.</param>
  107. /// <param name="state">A state object to be passed to <paramref name="action"/>.</param>
  108. /// <param name="dueTime">Relative time after which to execute the action.</param>
  109. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  110. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  111. internal static IDisposable ScheduleAction<TState>(this IScheduler scheduler, TState state, TimeSpan dueTime, Action<TState> action)
  112. {
  113. if (scheduler == null)
  114. {
  115. throw new ArgumentNullException(nameof(scheduler));
  116. }
  117. if (action == null)
  118. {
  119. throw new ArgumentNullException(nameof(action));
  120. }
  121. // See note above.
  122. return scheduler.Schedule((state, action), dueTime, static (s, tuple) => Invoke(s, tuple));
  123. }
  124. /// <summary>
  125. /// Schedules an action to be executed after the specified relative due time.
  126. /// </summary>
  127. /// <param name="scheduler">Scheduler to execute the action on.</param>
  128. /// <param name="action">Action to execute.</param>
  129. /// <param name="state">A state object to be passed to <paramref name="action"/>.</param>
  130. /// <param name="dueTime">Relative time after which to execute the action.</param>
  131. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  132. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  133. internal static IDisposable ScheduleAction<TState>(this IScheduler scheduler, TState state, TimeSpan dueTime, Func<TState, IDisposable> action)
  134. {
  135. if (scheduler == null)
  136. throw new ArgumentNullException(nameof(scheduler));
  137. if (action == null)
  138. throw new ArgumentNullException(nameof(action));
  139. // See note above.
  140. return scheduler.Schedule((state, action), dueTime, static (s, tuple) => Invoke(s, tuple));
  141. }
  142. /// <summary>
  143. /// Schedules an action to be executed at the specified absolute due time.
  144. /// </summary>
  145. /// <param name="scheduler">Scheduler to execute the action on.</param>
  146. /// <param name="action">Action to execute.</param>
  147. /// <param name="dueTime">Absolute time at which to execute the action.</param>
  148. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  149. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  150. public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset dueTime, Action action)
  151. {
  152. if (scheduler == null)
  153. {
  154. throw new ArgumentNullException(nameof(scheduler));
  155. }
  156. if (action == null)
  157. {
  158. throw new ArgumentNullException(nameof(action));
  159. }
  160. // See note above.
  161. return scheduler.Schedule(action, dueTime, static (s, a) => Invoke(s, a));
  162. }
  163. /// <summary>
  164. /// Schedules an action to be executed after the specified relative due time.
  165. /// </summary>
  166. /// <param name="scheduler">Scheduler to execute the action on.</param>
  167. /// <param name="action">Action to execute.</param>
  168. /// <param name="state">A state object to be passed to <paramref name="action"/>.</param>
  169. /// <param name="dueTime">Relative time after which to execute the action.</param>
  170. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  171. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  172. internal static IDisposable ScheduleAction<TState>(this IScheduler scheduler, TState state, DateTimeOffset dueTime, Action<TState> action)
  173. {
  174. if (scheduler == null)
  175. {
  176. throw new ArgumentNullException(nameof(scheduler));
  177. }
  178. if (action == null)
  179. {
  180. throw new ArgumentNullException(nameof(action));
  181. }
  182. // See note above.
  183. return scheduler.Schedule((state, action), dueTime, static (s, tuple) => Invoke(s, tuple));
  184. }
  185. /// <summary>
  186. /// Schedules an action to be executed after the specified relative due time.
  187. /// </summary>
  188. /// <param name="scheduler">Scheduler to execute the action on.</param>
  189. /// <param name="action">Action to execute.</param>
  190. /// <param name="state">A state object to be passed to <paramref name="action"/>.</param>
  191. /// <param name="dueTime">Relative time after which to execute the action.</param>
  192. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  193. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  194. internal static IDisposable ScheduleAction<TState>(this IScheduler scheduler, TState state, DateTimeOffset dueTime, Func<TState, IDisposable> action)
  195. {
  196. if (scheduler == null)
  197. throw new ArgumentNullException(nameof(scheduler));
  198. if (action == null)
  199. throw new ArgumentNullException(nameof(action));
  200. // See note above.
  201. return scheduler.Schedule((state, action), dueTime, static (s, tuple) => Invoke(s, tuple));
  202. }
  203. /// <summary>
  204. /// Schedules an action to be executed.
  205. /// </summary>
  206. /// <param name="scheduler">Scheduler to execute the action on.</param>
  207. /// <param name="action">Action to execute.</param>
  208. /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
  209. /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
  210. public static IDisposable ScheduleLongRunning(this ISchedulerLongRunning scheduler, Action<ICancelable> action)
  211. {
  212. if (scheduler == null)
  213. {
  214. throw new ArgumentNullException(nameof(scheduler));
  215. }
  216. if (action == null)
  217. {
  218. throw new ArgumentNullException(nameof(action));
  219. }
  220. return scheduler.ScheduleLongRunning(action, static (a, c) => a(c));
  221. }
  222. private static IDisposable Invoke(IScheduler scheduler, Action action)
  223. {
  224. action();
  225. return Disposable.Empty;
  226. }
  227. private static IDisposable Invoke<TState>(IScheduler scheduler, (TState state, Action<TState> action) tuple)
  228. {
  229. tuple.action(tuple.state);
  230. return Disposable.Empty;
  231. }
  232. private static IDisposable Invoke<TState>(IScheduler scheduler, (TState state, Func<TState, IDisposable> action) tuple)
  233. {
  234. return tuple.action(tuple.state);
  235. }
  236. }
  237. }