dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'dispatcher')

Error message

Value cannot be null. (Parameter 'dispatcher')

What it means

The DispatcherScheduler constructor validates its dispatcher parameter and throws ArgumentNullException if null. A scheduler without a Dispatcher cannot schedule any work, so the library rejects it immediately.

Solutions

  1. Pass a valid dispatcher, e.g. Application.Current.Dispatcher or theWindow.Dispatcher.
  2. Ensure the control is constructed/loaded before reading its Dispatcher property.
  3. Null-check the dispatcher source before constructing the scheduler and fall back to Application.Current.Dispatcher.

Example fix

// before
var sched = new DispatcherScheduler(myControl.Dispatcher); // may be null if control not loaded
// after
var dispatcher = myControl?.Dispatcher ?? Application.Current?.Dispatcher ?? throw new InvalidOperationException("no UI dispatcher available");
var sched = new DispatcherScheduler(dispatcher);
Defensive patterns

Strategy: validation

Validate before calling

if (dispatcher == null) dispatcher = Application.Current?.Dispatcher ?? throw new InvalidOperationException("no UI dispatcher");

Type guard

bool IsValidDispatcher(System.Windows.Threading.Dispatcher d) => d is not null;

Try / catch

try { var sched = new DispatcherScheduler(dispatcher); } catch (ArgumentNullException ex) when (ex.ParamName == "dispatcher") { /* resolve from Application.Current */ }

Prevention

When it happens

Trigger: new DispatcherScheduler(null), typically from a property/field such as SomeElement.Dispatcher read before the element is attached to the visual tree, or a Dispatcher-returning helper returning null.

Common situations: Reading control.Dispatcher in a constructor before WPF assigns it; DI-injected dispatcher that was never registered; using Dispatcher.FromThread on a non-UI thread (returns null) and feeding the result into the constructor.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive.Wpf/System.Reactive.Concurrency/DispatcherScheduler.cs:48

        /// </summary>
        public static DispatcherScheduler Current
        {
            get
            {
                var dispatcher = System.Windows.Threading.Dispatcher.FromThread(Thread.CurrentThread)
                    ?? throw new InvalidOperationException(Strings_Wpf_WindowsThreading.NO_DISPATCHER_CURRENT_THREAD);
                return new DispatcherScheduler(dispatcher);
            }
        }

        /// <summary>
        /// Constructs a <see cref="DispatcherScheduler"/> that schedules units of work on the given <see cref="System.Windows.Threading.Dispatcher"/>.
        /// </summary>
        /// <param name="dispatcher"><see cref="DispatcherScheduler"/> to schedule work on.</param>
        /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is <c>null</c>.</exception>
        public DispatcherScheduler(System.Windows.Threading.Dispatcher dispatcher)
        {
            Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
            Priority = System.Windows.Threading.DispatcherPriority.Normal;

        }

        /// <summary>
        /// Constructs a <see cref="DispatcherScheduler"/> that schedules units of work on the given <see cref="System.Windows.Threading.Dispatcher"/> at the given priority.
        /// </summary>
        /// <param name="dispatcher"><see cref="DispatcherScheduler"/> to schedule work on.</param>
        /// <param name="priority">Priority at which units of work are scheduled.</param>
        /// <exception cref="ArgumentNullException"><paramref name="dispatcher"/> is <c>null</c>.</exception>
        public DispatcherScheduler(System.Windows.Threading.Dispatcher dispatcher, System.Windows.Threading.DispatcherPriority priority)
        {
            Dispatcher = dispatcher ?? throw new ArgumentNullException(nameof(dispatcher));
            Priority = priority;
        }

        /// <summary>
        /// Gets the <see cref="System.Windows.Threading.Dispatcher"/> associated with the <see cref="DispatcherScheduler"/>.

View on GitHub (pinned to 94b5d5ab91)