From 430ba8f198b0d111f5274432065c6a542d853a12 Mon Sep 17 00:00:00 2001 From: Daniel Weber Date: Thu, 28 Jun 2018 13:46:14 +0200 Subject: [PATCH] Add timed non recursive scheduling extensions. --- .../Concurrency/Scheduler.Simple.cs | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Simple.cs b/Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Simple.cs index 1259a4fb74..bf6d5788eb 100644 --- a/Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Simple.cs +++ b/Rx.NET/Source/src/System.Reactive/Concurrency/Scheduler.Simple.cs @@ -76,6 +76,26 @@ public static IDisposable Schedule(this IScheduler scheduler, TimeSpan dueTime, return scheduler.Schedule(action, dueTime, (s, a) => Invoke(s, a)); } + /// + /// Schedules an action to be executed after the specified relative due time. + /// + /// Scheduler to execute the action on. + /// Action to execute. + /// A state object to be passed to . + /// Relative time after which to execute the action. + /// The disposable object used to cancel the scheduled action (best effort). + /// or is null. + internal static IDisposable ScheduleAction(this IScheduler scheduler, TState state, TimeSpan dueTime, Action 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)); + } + /// /// Schedules an action to be executed at the specified absolute due time. /// @@ -95,6 +115,26 @@ public static IDisposable Schedule(this IScheduler scheduler, DateTimeOffset due return scheduler.Schedule(action, dueTime, (s, a) => Invoke(s, a)); } + /// + /// Schedules an action to be executed after the specified relative due time. + /// + /// Scheduler to execute the action on. + /// Action to execute. + /// A state object to be passed to . + /// Relative time after which to execute the action. + /// The disposable object used to cancel the scheduled action (best effort). + /// or is null. + internal static IDisposable ScheduleAction(this IScheduler scheduler, TState state, DateTimeOffset dueTime, Action 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)); + } + /// /// Schedules an action to be executed. /// @@ -117,5 +157,11 @@ private static IDisposable Invoke(IScheduler scheduler, Action action) action(); return Disposable.Empty; } + + private static IDisposable Invoke(IScheduler scheduler, (TState state, Action action) tuple) + { + tuple.action(tuple.state); + return Disposable.Empty; + } } }