dotnet/reactive · error · ArgumentNullException

dispatcher (Parameter 'dispatcher')

Error message

dispatcher (Parameter 'dispatcher')

What it means

ObserveOn needs a CoreDispatcher to schedule notifications on the UI thread; a null dispatcher cannot be used to create the CoreDispatcherScheduler. ArgumentNullException with ParamName 'dispatcher' is thrown by the second guard. The dispatcher is required at call time, not at subscription time.

Solutions

  1. Obtain the dispatcher on the UI thread (e.g. CoreApplication.MainView.CoreWindow.Dispatcher) and pass it after ensuring it is non-null.
  2. Capture the dispatcher during app startup (OnLaunched) and reuse that cached value.
  3. Add a guard: if (dispatcher == null) throw/log before calling ObserveOn, or fall back to the current scheduler.

Example fix

// before
var d = CoreWindow.GetForCurrentThread()?.Dispatcher;
stream.ObserveOn(d).Subscribe(...); // d may be null off the UI thread
// after
var d = CoreApplication.MainView.CoreWindow.Dispatcher;
if (d != null) stream.ObserveOn(d).Subscribe(...);
Defensive patterns

Strategy: validation

Validate before calling

var dispatcher = CoreApplication.MainView.CoreWindow?.Dispatcher;
if (dispatcher == null) { /* defer or log; do not call ObserveOn */ }

Type guard

bool HasDispatcher(CoreDispatcher d) => d is not null;

Try / catch

try { stream.ObserveOn(dispatcher).Subscribe(onNext); }
catch (ArgumentNullException ex) when (ex.ParamName == "dispatcher") { /* subscribe without marshalling or retry on UI thread */ }

Prevention

When it happens

Trigger: Calling source.ObserveOn(dispatcher) where the CoreDispatcher came from a Window/CoreWindow that is null (e.g. code running on a background thread without a window, or CoreWindow.Active captured before activation).

Common situations: Calling from a background thread where CoreWindow.GetCurrentView() throws or returns null; caching a dispatcher in a static field before the window exists; app shutdown after the window is closed.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive/Platforms/WinRT/Linq/CoreDispatcherObservable.cs:40

        /// <summary>
        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="dispatcher">Dispatcher whose associated message loop is used to notify observers on.</param>
        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, CoreDispatcher dispatcher)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            if (dispatcher == null)
            {
                throw new ArgumentNullException(nameof(dispatcher));
            }

            return Synchronization.ObserveOn(source, new CoreDispatcherScheduler(dispatcher));
        }

        /// <summary>
        /// Wraps the source sequence in order to run its observer callbacks on the specified dispatcher.
        /// </summary>
        /// <typeparam name="TSource">The type of the elements in the source sequence.</typeparam>
        /// <param name="source">Source sequence.</param>
        /// <param name="dispatcher">Dispatcher whose associated message loop is used to notify observers on.</param>
        /// <param name="priority">Priority to schedule work items at.</param>
        /// <returns>The source sequence whose observations happen on the specified dispatcher.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="source"/> or <paramref name="dispatcher"/> is null.</exception>
        public static IObservable<TSource> ObserveOn<TSource>(this IObservable<TSource> source, CoreDispatcher dispatcher, CoreDispatcherPriority priority)
        {
            if (source == null)
            {

View on GitHub (pinned to 94b5d5ab91)