dotnet/wpf · error · InvalidOperationException
SR.DispatcherYieldNoAvailableDispatcher
Error message
SR.DispatcherYieldNoAvailableDispatcher
What it means
Dispatcher.Yield throws InvalidOperationException with SR.DispatcherYieldNoAvailableDispatcher when the calling thread has no Dispatcher (FromThread returned null). Yield is meant to suspend the current dispatcher's processing and let lower-priority work run, so it is meaningless on a thread that has no message pump.
Solutions
- Ensure the calling thread has a dispatcher before yielding: touch Dispatcher.CurrentDispatcher and pump with Dispatcher.Run/PushFrame, or move the code to the UI thread.
- Replace Dispatcher.Yield with await Task.Yield() on non-dispatcher threads.
- Marshal to the application's UI dispatcher (Dispatcher.InvokeAsync) before yielding.
- Wrap in a check: if (Dispatcher.FromThread(Thread.CurrentThread) == null) use Task.Yield instead.
Example fix
// before
await Dispatcher.Yield(); // on a thread-pool thread
// after
await Task.Yield(); // or run on the UI dispatcher:
await Application.Current.Dispatcher.InvokeAsync(() => { ... }); Defensive patterns
Strategy: validation
Validate before calling
if (Dispatcher.FromThread(Thread.CurrentThread) == null)
{
await Task.Yield();
}
else
{
await Dispatcher.Yield();
} Type guard
bool HasCurrentDispatcher => Dispatcher.FromThread(Thread.CurrentThread) != null;
Prevention
- Use Dispatcher.Yield only on threads with a running Dispatcher (usually the UI thread)
- Use await Task.Yield() on thread-pool threads
- Marshal onto Application.Current.Dispatcher before dispatcher-specific awaits
- Avoid calling UI-helper library methods from background threads
When it happens
Trigger: Calling Dispatcher.Yield() (or Yield(priority)) from a background/thread-pool thread that never created a Dispatcher, i.e. no Dispatcher.Run or Dispatcher.CurrentDispatcher access happened on that thread.
Common situations: Using `await Dispatcher.Yield()` inside Task.Run lambdas or thread-pool code; library code written for the UI thread being reused on non-dispatcher threads.
Understand the failure class
Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.
Related errors
- Processing is disabled while the Dispatcher is in this…
- SR.CollectionView_MissingSynchronizationCallback
- SR.ContextMenuInDifferentDispatcher
- SR.CurrentDispatcherNotFound
- SR.DispatcherHasShutdown
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/9a34448955412e41.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs:357
/// continuations at background priority.
/// </summary>
public static DispatcherPriorityAwaitable Yield()
{
return Yield(DispatcherPriority.Background);
}
/// <summary>
/// Returns an awaitable object that can be used to queue
/// continuations at the specified priority.
/// </summary>
public static DispatcherPriorityAwaitable Yield(DispatcherPriority priority)
{
ValidatePriority(priority, "priority");
Dispatcher currentDispatcher = FromThread(Thread.CurrentThread);;
if(currentDispatcher == null)
{
throw new InvalidOperationException(SR.DispatcherYieldNoAvailableDispatcher);
}
return new DispatcherPriorityAwaitable(currentDispatcher, priority);
}
/// <summary>
/// Executes the specified delegate asynchronously on the thread that
/// the Dispatcher was created on.
/// </summary>
/// <param name="priority">
/// The priority that determines in what order the specified method
/// is invoked relative to the other pending methods in the Dispatcher.
/// </param>
/// <param name="method">
/// A delegate to a method that takes no parameters.
/// </param>
/// <returns>
/// An IAsyncResult object that represents the result of theView on GitHub (pinned to 81131a70a4)