dotnet/reactive · error · ArgumentOutOfRangeException

ArgumentOutOfRangeException: period (Specified argument was…

Error message

ArgumentOutOfRangeException: period (Specified argument was out of the range of valid values.)

What it means

SchedulePeriodic validates that the repetition period is non-negative; a negative TimeSpan is rejected with ArgumentOutOfRangeException naming period. WinRT dispatcher timers cannot meaningfully represent a negative tick interval, so the library fails fast rather than silently normalizing.

Solutions

  1. Clamp the period: if (period < TimeSpan.Zero) period = TimeSpan.Zero; before calling.
  2. Fix the interval computation so it cannot produce negative values.
  3. Validate configured interval values at startup and reject non-positive settings early.

Example fix

// before
scheduler.SchedulePeriodic(state, TimeSpan.FromMilliseconds(timeoutMs), action); // timeoutMs can be negative
// after
var period = TimeSpan.FromMilliseconds(Math.Max(0, timeoutMs));
scheduler.SchedulePeriodic(state, period, action);
Defensive patterns

Strategy: validation

Validate before calling

if (period < TimeSpan.Zero) period = TimeSpan.Zero;
scheduler.SchedulePeriodic(state, period, action);

Type guard

bool IsValidPeriod(TimeSpan p) => p >= TimeSpan.Zero;

Try / catch

try { scheduler.SchedulePeriodic(state, period, action); }
catch (ArgumentOutOfRangeException ex) when (ex.ParamName == "period")
{ /* clamp period and retry */ }

Prevention

When it happens

Trigger: Calling scheduler.SchedulePeriodic(state, TimeSpan.FromSeconds(-1), action) or computing a period from data that can go negative (e.g. remaining = deadline - now with clock skew).

Common situations: Dynamic poll intervals derived from configuration or measurements that underflow to negative values; sign errors when computing delays.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive.WindowsRuntime/System.Reactive.Concurrency/CoreDispatcherScheduler.cs:244

        /// <summary>
        /// Schedules a periodic piece of work on the dispatcher, using a <see cref="DispatcherQueueTimer"/> 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)
        {
            //
            // According to MSDN documentation, the default is TimeSpan.Zero, so that's definitely valid.
            // Empirical observation - negative values seem to be normalized to TimeSpan.Zero, but let's not go there.
            //
            if (period < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(period));
            }

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

            var timer = CreateDispatcherQueue().CreateTimer();

            var state1 = state;

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

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

View on GitHub (pinned to 94b5d5ab91)