dotnet/reactive · error · ArgumentNullException

Value cannot be null. (Parameter 'dispatcher')

Error message

Value cannot be null. (Parameter 'dispatcher')

What it means

The DispatcherScheduler(System.Windows.Threading.Dispatcher) constructor requires a non-null dispatcher because all work is queued through it. Passing null throws ArgumentNullException with Parameter 'dispatcher'.

Solutions

  1. Pass a live dispatcher such as Application.Current.Dispatcher or Dispatcher.CurrentDispatcher (on the UI thread).
  2. If you have a Thread, verify Dispatcher.FromThread(thread) is non-null before constructing.
  3. Guard with ?? throw or a fallback scheduler when the dispatcher may be unavailable.

Example fix

// before
var dispatcher = Dispatcher.FromThread(uiThread); // null if no dispatcher on that thread
var scheduler = new DispatcherScheduler(dispatcher);
// after
var dispatcher = Application.Current?.Dispatcher ?? Dispatcher.CurrentDispatcher;
var scheduler = new DispatcherScheduler(dispatcher);
Defensive patterns

Strategy: validation

Validate before calling

var dispatcher = Application.Current?.Dispatcher ?? System.Windows.Threading.Dispatcher.CurrentDispatcher;
if (dispatcher == null) { /* fallback */ }
var scheduler = new DispatcherScheduler(dispatcher);

Type guard

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

Try / catch

try { var s = new DispatcherScheduler(dispatcher); } catch (ArgumentNullException ex) when (ex.ParamName == "dispatcher") { /* use Scheduler.Default */ }

Prevention

When it happens

Trigger: new DispatcherScheduler(null), or passing a dispatcher obtained from Dispatcher.FromThread(t) where thread t has no Dispatcher, or Application.Current.Dispatcher when Application.Current is null (non-WPF context).

Common situations: Resolving the dispatcher from an arbitrary worker thread whose FromThread result is null; calling Application.Current.Dispatcher in unit tests or a WPF-less host; dependency-injection registrations that yield a null dispatcher.

Related errors


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

Appendix: source

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

        /// </summary>
        public static DispatcherScheduler Current
        {
            get
            {
                var dispatcher = System.Windows.Threading.Dispatcher.FromThread(Thread.CurrentThread)
                    ?? throw new InvalidOperationException(Strings_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)