dotnet/reactive · error · ArgumentNullException

ArgumentNullException: dispatcher

Error message

ArgumentNullException: dispatcher

What it means

ObserveOn(source, CoreDispatcher) also validates the dispatcher and throws ArgumentNullException('dispatcher') when it is null. The dispatcher identifies the thread on which notifications are delivered, so it is required.

Solutions

  1. Obtain the dispatcher correctly: Window.Current.Dispatcher (UI thread) or CoreWindow.GetForCurrentThread().Dispatcher.
  2. Cache the dispatcher while on the UI thread (e.g. in App startup) rather than fetching it from a background thread.
  3. Use ObserveOnDispatcher()/ObserveOnCoreDispatcher() when you simply want the current window's dispatcher.

Example fix

// before
CoreDispatcher dispatcher = Window.Current?.Dispatcher; // null off UI thread
stream.ObserveOn(dispatcher);
// after
var dispatcher =CoreApplication.MainView.CoreWindow.Dispatcher; // or capture on UI thread at startup
stream.ObserveOn(dispatcher);
Defensive patterns

Strategy: validation

Validate before calling

var dispatcher = Window.Current?.Dispatcher;
if (dispatcher is null)
{
    // not on the UI thread — capture earlier or fall back
    subscribeOnDefault = true;
}
else
{
    stream.ObserveOn(dispatcher).Subscribe(OnNext);
}

Type guard

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

Try / catch

try
{
    stream.ObserveOn(dispatcher).Subscribe(OnNext);
}
catch (ArgumentNullException ex) when (ex.ParamName == "dispatcher")
{
    log.Warn("CoreDispatcher unavailable (off UI thread or window closed).");
}

Prevention

When it happens

Trigger: Calling source.ObserveOn(null) — common when Window.Current/CoreWindow is not available (e.g. code running in a background thread, a host window closed, or app suspending).

Common situations: UWP apps accessing CoreDispatcher from a non-UI thread where Window.Current is null; sharing a cached dispatcher field that was disposed or never initialized; unit tests without a dispatcher.

Related errors


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

Appendix: source

Thrown at Rx.NET/Source/src/System.Reactive.WindowsRuntime/System.Reactive.Linq/CoreDispatcherObservable.cs:39

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