1
0
Эх сурвалжийг харах

Add timed non recursive scheduling extensions. (#687)

Daniel C. Weber 7 жил өмнө
parent
commit
2d4f71e13f

+ 46 - 0
Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Simple.cs

@@ -76,6 +76,26 @@ namespace System.Reactive.Concurrency
             return scheduler.Schedule(action, dueTime, (s, a) => Invoke(s, a));
         }
 
+        /// <summary>
+        /// Schedules an action to be executed after the specified relative due time.
+        /// </summary>
+        /// <param name="scheduler">Scheduler to execute the action on.</param>
+        /// <param name="action">Action to execute.</param>
+        /// <param name="state">A state object to be passed to <paramref name="action"/>.</param>
+        /// <param name="dueTime">Relative time after which to execute the action.</param>
+        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
+        /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
+        internal static IDisposable ScheduleAction<TState>(this IScheduler scheduler, TState state, TimeSpan dueTime, Action<TState> action)
+        {
+            if (scheduler == null)
+                throw new ArgumentNullException(nameof(scheduler));
+            if (action == null)
+                throw new ArgumentNullException(nameof(action));
+
+            // See note above.
+            return scheduler.Schedule((state, action), dueTime, (s, tuple) => Invoke(s, tuple));
+        }
+
         /// <summary>
         /// Schedules an action to be executed at the specified absolute due time.
         /// </summary>
@@ -95,6 +115,26 @@ namespace System.Reactive.Concurrency
             return scheduler.Schedule(action, dueTime, (s, a) => Invoke(s, a));
         }
 
+        /// <summary>
+        /// Schedules an action to be executed after the specified relative due time.
+        /// </summary>
+        /// <param name="scheduler">Scheduler to execute the action on.</param>
+        /// <param name="action">Action to execute.</param>
+        /// <param name="state">A state object to be passed to <paramref name="action"/>.</param>
+        /// <param name="dueTime">Relative time after which to execute the action.</param>
+        /// <returns>The disposable object used to cancel the scheduled action (best effort).</returns>
+        /// <exception cref="ArgumentNullException"><paramref name="scheduler"/> or <paramref name="action"/> is <c>null</c>.</exception>
+        internal static IDisposable ScheduleAction<TState>(this IScheduler scheduler, TState state, DateTimeOffset dueTime, Action<TState> action)
+        {
+            if (scheduler == null)
+                throw new ArgumentNullException(nameof(scheduler));
+            if (action == null)
+                throw new ArgumentNullException(nameof(action));
+
+            // See note above.
+            return scheduler.Schedule((state, action), dueTime, (s, tuple) => Invoke(s, tuple));
+        }
+
         /// <summary>
         /// Schedules an action to be executed.
         /// </summary>
@@ -117,5 +157,11 @@ namespace System.Reactive.Concurrency
             action();
             return Disposable.Empty;
         }
+
+        private static IDisposable Invoke<TState>(IScheduler scheduler, (TState state, Action<TState> action) tuple)
+        {
+            tuple.action(tuple.state);
+            return Disposable.Empty;
+        }
     }
 }