{"record":{"id":"bea9705a3ff8327a","repo":"dotnet/wpf","slug":"sr-contextmenuindifferentdispatcher","errorCode":null,"errorMessage":"SR.ContextMenuInDifferentDispatcher","messagePattern":"SR\\.ContextMenuInDifferentDispatcher","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ContextMenuService.cs","lineNumber":43,"sourceCode":"                        typeof(ContextMenuService), // Owner\n                        new FrameworkPropertyMetadata((ContextMenu)null,\n                                FrameworkPropertyMetadataOptions.None));\n\n        /// <summary>\n        ///     Gets the value of the ContextMenu property on the specified object.\n        /// </summary>\n        /// <param name=\"element\">The object on which to query the ContextMenu property.</param>\n        /// <returns>The value of the ContextMenu property.</returns>\n        [AttachedPropertyBrowsableForType(typeof(DependencyObject))]\n        public static ContextMenu GetContextMenu(DependencyObject element)\n        {\n            ArgumentNullException.ThrowIfNull(element);\n\n            ContextMenu cm = (ContextMenu)element.GetValue(ContextMenuProperty);\n\n            if ((cm != null) && (element.Dispatcher != cm.Dispatcher))\n            {\n                throw new ArgumentException(SR.ContextMenuInDifferentDispatcher);\n            }\n\n            return cm;\n       }\n\n        /// <summary>\n        ///     Sets the ContextMenu property on the specified object.\n        /// </summary>\n        /// <param name=\"element\">The object on which to set the ContextMenu property.</param>\n        /// <param name=\"value\">\n        ///     The value of the ContextMenu property. If the value is of type ContextMenu, then\n        ///     that is the ContextMenu that will be used (without any modification). If the value\n        ///     is of any other type, then that value will be used as the content for a ContextMenu\n        ///     provided by this service, and the other attached properties of this service\n        ///     will be used to configure the ContextMenu.\n        /// </param>\n        public static void SetContextMenu(DependencyObject element, ContextMenu value)\n        {","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ContextMenuService.cs#L25-L61","documentation":"ContextMenuService.GetContextMenu reads the ContextMenu attached property from an element and throws ArgumentException(SR.ContextMenuInDifferentDispatcher) when the retrieved menu is non-null but belongs to a different Dispatcher (i.e., was created on another UI thread). Cross-thread use of a DependencyObject is forbidden in WPF.","triggerScenarios":"Calling ContextMenuService.GetContextMenu(element) where element.GetValue(ContextMenuProperty) returns a ContextMenu whose Dispatcher differs from element.Dispatcher — typically the menu was created on another UI thread.","commonSituations":"Multi-UI-thread WPF apps (multiple windows each with own Dispatcher) where a ContextMenu instance created on one thread is assigned to an element on another, or shared resource dictionaries across threads.","solutions":["Create the ContextMenu on the same thread (Dispatcher) as the element it will be attached to.","Marshal work back with element.Dispatcher.Invoke/BeginInvoke before reading or assigning the menu.","Rebuild the menu on the target thread instead of sharing a single instance across threads.","Check cm.Dispatcher == element.Dispatcher before calling GetContextMenu if the menu's origin is uncertain."],"exampleFix":"// before\nvar menu = ContextMenuService.GetContextMenu(element); // menu on other thread\n// after\nContextMenu menu = null;\nelement.Dispatcher.Invoke(() => menu = ContextMenuService.GetContextMenu(element));","handlingStrategy":"validation","validationCode":"ContextMenu cm = (ContextMenu)element.GetValue(ContextMenuService.ContextMenuProperty);\nbool safe = cm == null || cm.Dispatcher == element.Dispatcher;","typeGuard":"static bool IsSameDispatcher(ContextMenu cm, DependencyObject el) => cm == null || cm.Dispatcher == el.Dispatcher;","tryCatchPattern":"try\n{\n    var menu = ContextMenuService.GetContextMenu(element);\n}\ncatch (ArgumentException)\n{\n    // menu belongs to another Dispatcher; rebuild it on element's thread\n}","preventionTips":["Create UI objects on the thread that owns the element","Use Dispatcher.Invoke when touching objects across threads","Avoid sharing ContextMenu instances between windows on different Dispatchers"],"tags":["wpf","threading","dispatcher","contextmenu"],"backgroundTag":"thread-interrupted","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"}