{"record":{"id":"6c6e085e9abe2c21","repo":"dotnet/wpf","slug":"sr-mismatcheddispatchers","errorCode":null,"errorMessage":"SR.MismatchedDispatchers","messagePattern":"SR\\.MismatchedDispatchers","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs","lineNumber":309,"sourceCode":"        /// <summary>\n        ///     Push an execution frame.\n        /// </summary>\n        /// <param name=\"frame\">\n        ///     The frame for the dispatcher to process.\n        /// </param>\n        public static void PushFrame(DispatcherFrame frame)\n        {\n            ArgumentNullException.ThrowIfNull(frame);\n\n            Dispatcher dispatcher = Dispatcher.CurrentDispatcher;\n            if(dispatcher._hasShutdownFinished) // Dispatcher thread - no lock needed for read\n            {\n                throw new InvalidOperationException(SR.DispatcherHasShutdown);\n            }\n\n            if(frame.Dispatcher != dispatcher)\n            {\n                throw new InvalidOperationException(SR.MismatchedDispatchers);\n            }\n\n            if(dispatcher._disableProcessingCount > 0)\n            {\n                throw new InvalidOperationException(SR.DispatcherProcessingDisabled);\n            }\n\n            dispatcher.PushFrameImpl(frame);\n        }\n\n        /// <summary>\n        ///     Requests that all nested frames exit.\n        /// </summary>\n        public static void ExitAllFrames()\n        {\n\n            Dispatcher dispatcher = Dispatcher.CurrentDispatcher;\n            if(dispatcher._frameDepth > 0)","sourceCodeStart":291,"sourceCodeEnd":327,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Threading/Dispatcher.cs#L291-L327","documentation":"Dispatcher.PushFrame throws InvalidOperationException with SR.MismatchedDispatchers when the DispatcherFrame passed in was created for (or associated with) a different Dispatcher than the one running on the current thread. Each frame records its owning Dispatcher, and PushFrame validates frame.Dispatcher == Dispatcher.CurrentDispatcher before pushing.","triggerScenarios":"Calling PushFrame on thread A with a DispatcherFrame whose Dispatcher property points to thread B's dispatcher — typically a frame captured or created on another thread, or a shared static frame reused across threads.","commonSituations":"Caching a DispatcherFrame in a static/shared field and pumping it from multiple threads; creating a frame on the UI thread and pushing it from a worker; borrowing frames across modal-dialog helpers.","solutions":["Create a new DispatcherFrame on the same thread you call PushFrame from.","Ensure the frame's Dispatcher equals Dispatcher.CurrentDispatcher before pushing.","Stop sharing frame instances across threads; make frames thread-local.","If cross-thread work is intended, use Dispatcher.Invoke/InvokeAsync on the target dispatcher instead of pumping its frame from another thread."],"exampleFix":"// before\nprivate static readonly DispatcherFrame _frame = new DispatcherFrame();\nDispatcher.PushFrame(_frame);\n// after\nDispatcher.PushFrame(new DispatcherFrame()); // created on the calling thread","handlingStrategy":"validation","validationCode":"if (frame.Dispatcher != Dispatcher.CurrentDispatcher)\n    frame = new DispatcherFrame(); // recreate on this thread\nDispatcher.PushFrame(frame);","typeGuard":"bool IsFrameForCurrentThread(DispatcherFrame f) => f.Dispatcher == Dispatcher.CurrentDispatcher;","tryCatchPattern":null,"preventionTips":["Never cache DispatcherFrame instances in static/shared fields","Create frames at the point of use, on the calling thread","Document that frames are thread-affine like all Dispatcher objects","Prefer Invoke/InvokeAsync for cross-thread work instead of pumping another thread's dispatcher"],"tags":["wpf","dispatcher","threading","frame"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}