dotnet/reactive · error · ArgumentOutOfRangeException

period

Error message

period

What it means

DispatcherScheduler.SchedulePeriodic validates that the period is not negative before creating the WPF DispatcherTimer. A negative period is meaningless for a timer and is rejected with ArgumentOutOfRangeException. Zero and positive periods are allowed.

Solutions

  1. Pass a TimeSpan greater than or equal to TimeSpan.Zero as the period.
  2. Clamp with a check before calling: if (period < TimeSpan.Zero) period = TimeSpan.Zero;
  3. Fix the computation that produced the negative interval (e.g. swap operands of the subtraction).

Example fix

// before
scheduler.SchedulePeriodic(state, TimeSpan.FromSeconds(interval), action); // interval = -5
// after
var period = TimeSpan.FromSeconds(Math.Max(0, interval));
scheduler.SchedulePeriodic(state, period, action);
Defensive patterns

Strategy: validation

Validate before calling

if (period < TimeSpan.Zero)
    throw new ArgumentOutOfRangeException(nameof(period), period, "Period must be non-negative");
var d = dispatcherScheduler.SchedulePeriodic(state, period, action);

Type guard

static bool IsValidPeriod(TimeSpan period) => period >= TimeSpan.Zero;

Try / catch

try { var d = scheduler.SchedulePeriodic(state, period, action); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "period") { log.Error($"Bad period {ex.ActualValue}"); }

Prevention

When it happens

Trigger: Calling scheduler.SchedulePeriodic(state, TimeSpan.FromMilliseconds(-1), action) or computing the period from a subtraction that went negative, e.g. TimeSpan.FromTicks(endTicks - startTicks) where start > end.

Common situations: Config values read as negative intervals; misordered subtraction of DateTimes; default/unset interval values like -1 standing in for 'not configured'.

Related errors


AI-assisted analysis of dotnet/reactive@94b5d5ab91 (2026-09-15). Data as JSON: /api/errors/b61744a6e7ba28cd. Report an issue: GitHub.

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Platforms/Desktop/Concurrency/DispatcherScheduler.cs:181

            return d;
        }

        /// <summary>
        /// Schedules a periodic piece of work on the dispatcher, using a <see cref="System.Windows.Threading.DispatcherTimer"/> object.
        /// </summary>
        /// <typeparam name="TState">The type of the state passed to the scheduled action.</typeparam>
        /// <param name="state">Initial state passed to the action upon the first iteration.</param>
        /// <param name="period">Period for running the work periodically.</param>
        /// <param name="action">Action to be executed, potentially updating the state.</param>
        /// <returns>The disposable object used to cancel the scheduled recurring action (best effort).</returns>
        /// <exception cref="ArgumentNullException"><paramref name="action"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="period"/> is less than <see cref="TimeSpan.Zero"/>.</exception>
        public IDisposable SchedulePeriodic<TState>(TState state, TimeSpan period, Func<TState, TState> action)
        {
            if (period < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(period));
            }

            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }

            var timer = new System.Windows.Threading.DispatcherTimer(Priority, Dispatcher);

            var state1 = state;

            timer.Tick += (s, e) =>
            {
                state1 = action(state1);
            };

            timer.Interval = period;
            timer.Start();

View on GitHub (pinned to 94b5d5ab91)