{"record":{"id":"9a34448955412e41","repo":"dotnet/wpf","slug":"sr-dispatcheryieldnoavailabledispatcher","errorCode":null,"errorMessage":"SR.DispatcherYieldNoAvailableDispatcher","messagePattern":"SR\\.DispatcherYieldNoAvailableDispatcher","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs","lineNumber":357,"sourceCode":"        ///     continuations at background priority.\n        /// </summary>\n        public static DispatcherPriorityAwaitable Yield()\n        {\n            return Yield(DispatcherPriority.Background);\n        }\n\n        /// <summary>\n        ///     Returns an awaitable object that can be used to queue\n        ///     continuations at the specified priority.\n        /// </summary>\n        public static DispatcherPriorityAwaitable Yield(DispatcherPriority priority)\n        {\n            ValidatePriority(priority, \"priority\");\n\n            Dispatcher currentDispatcher = FromThread(Thread.CurrentThread);;\n            if(currentDispatcher == null)\n            {\n                throw new InvalidOperationException(SR.DispatcherYieldNoAvailableDispatcher);\n            }\n\n            return new DispatcherPriorityAwaitable(currentDispatcher, priority);\n        }\n\n        /// <summary>\n        ///     Executes the specified delegate asynchronously on the thread that\n        ///     the Dispatcher was created on.\n        /// </summary>\n        /// <param name=\"priority\">\n        ///     The priority that determines in what order the specified method\n        ///     is invoked relative to the other pending methods in the Dispatcher.\n        /// </param>\n        /// <param name=\"method\">\n        ///     A delegate to a method that takes no parameters.\n        /// </param>\n        /// <returns>\n        ///     An IAsyncResult object that represents the result of the","sourceCodeStart":339,"sourceCodeEnd":375,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs#L339-L375","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nawait Dispatcher.Yield(); // on a thread-pool thread\n// after\nawait Task.Yield(); // or run on the UI dispatcher:\nawait Application.Current.Dispatcher.InvokeAsync(() => { ... });","handlingStrategy":"validation","validationCode":"if (Dispatcher.FromThread(Thread.CurrentThread) == null)\n{\n    await Task.Yield();\n}\nelse\n{\n    await Dispatcher.Yield();\n}","typeGuard":"bool HasCurrentDispatcher => Dispatcher.FromThread(Thread.CurrentThread) != null;","tryCatchPattern":null,"preventionTips":["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"],"tags":["wpf","dispatcher","threading","yield"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}