dotnet/reactive · error · ArgumentNullException

action

Error message

action

What it means

After validating the period, SchedulePeriodic checks the action delegate for null because the periodic DispatcherTimer tick will invoke it on every interval. A null action would crash inside the timer callback, so it throws ArgumentNullException up front.

Solutions

  1. Pass a non-null Func<TState, TState> to SchedulePeriodic.
  2. Initialize the delegate before starting the periodic schedule.
  3. Return an identity function (st => st) if the state does not need updating each tick.

Example fix

// before
scheduler.SchedulePeriodic(state, period, periodicAction); // periodicAction is null
// after
periodicAction ??= st => st;
var d = scheduler.SchedulePeriodic(state, period, periodicAction);
Defensive patterns

Strategy: validation

Validate before calling

if (action == null) throw new InvalidOperationException("Periodic schedule requires a non-null action");
if (period < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(period));
var d = dispatcherScheduler.SchedulePeriodic(state, period, action);

Type guard

static bool IsValidPeriodic<TState>(TimeSpan period, Func<TState, TState> action) => period >= TimeSpan.Zero && action is not null;

Try / catch

try { var d = scheduler.SchedulePeriodic(state, period, action); }
catch (ArgumentNullException ex) when (ex.ParamName == "action") { log.Error("Null periodic action"); }

Prevention

When it happens

Trigger: Calling scheduler.SchedulePeriodic(state, TimeSpan.FromSeconds(1), null) or passing a Func<TState, TState> field/variable that was never assigned.

Common situations: Periodic-poll wrappers whose callback is supplied conditionally; delegates captured from event handlers that have not fired yet; deserialized configuration objects with null function members.

Related errors


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

Appendix: source

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

        /// 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();

            return Disposable.Create(() =>
            {
                var t = Interlocked.Exchange(ref timer, null);
                if (t != null)

View on GitHub (pinned to 94b5d5ab91)