dotnet/reactive · error · ArgumentNullException

action (Parameter 'action')

Error message

action (Parameter 'action')

What it means

CoreDispatcherScheduler.SchedulePeriodic<TState>(state, period, action) throws ArgumentNullException because the periodic 'action' delegate is null. The periodic timer invokes this delegate on each tick, so a null delegate is rejected before the WinRT DispatcherQueueTimer is created.

Solutions

  1. Supply a non-null action delegate to SchedulePeriodic.
  2. Guard the call site: only call SchedulePeriodic when the periodic callback has been initialized.
  3. Centralize delegate creation so the periodic callback cannot be null.

Example fix

// before
Func<IScheduler, int, IDisposable> tick = null;
scheduler.SchedulePeriodic(0, TimeSpan.FromSeconds(1), tick);
// after
Func<IScheduler, int, IDisposable> tick = (s, st) => { DoWork(); return Disposable.Empty; };
scheduler.SchedulePeriodic(0, TimeSpan.FromSeconds(1), tick);
Defensive patterns

Strategy: validation

Validate before calling

// csharp
if (tickAction == null)
    throw new ArgumentException("Periodic action must be provided", nameof(tickAction));
d = scheduler.SchedulePeriodic(state, period, tickAction);

Type guard

// csharp
bool CanSchedulePeriodic<TState>(Func<IScheduler, TState, IDisposable> action) => action is not null;

Try / catch

// csharp
try { d = scheduler.SchedulePeriodic(state, period, action); }
catch (ArgumentNullException ex) when (ex.ParamName == "action") { log.Warn("Periodic work not scheduled: null action"); d = Disposable.Empty; }

Prevention

When it happens

Trigger: Calling SchedulePeriodic with a null Func<IScheduler,TState,IDisposable>, e.g. an uninitialized member or a lookup returning null for the periodic work function.

Common situations: Same as other null-action cases: nullable delegate fields not yet assigned, factory methods returning null, or code paths where the periodic callback is only conditionally created.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Concurrency/CoreDispatcherScheduler.cs:239

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

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

View on GitHub (pinned to 94b5d5ab91)