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
- Supply a non-null action delegate to SchedulePeriodic.
- Guard the call site: only call SchedulePeriodic when the periodic callback has been initialized.
- 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
- Create periodic callbacks as constants/lambdas at the call site instead of nullable variables
- Centralize periodic-work registration in one helper that validates arguments
- Enable nullable reference type warnings project-wide
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
- throw new ArgumentNullException(nameof(scheduler));
- Value cannot be null. (Parameter 'scheduler')
- source
- scheduler (Value cannot be null)
- action (Value cannot be null)
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)