{"record":{"id":"320f2e753f3b0f1a","repo":"dotnet/wpf","slug":"sr-compatibilitypreferencessealed-320f2e","errorCode":null,"errorMessage":"SR.CompatibilityPreferencesSealed (ReuseDispatcherSynchronizationContextInstance, BaseCompatibilityPreferences)","messagePattern":"SR\\.CompatibilityPreferencesSealed \\(ReuseDispatcherSynchronizationContextInstance, BaseCompatibilityPreferences\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/BaseCompatibilityPreferences.cs","lineNumber":84,"sourceCode":"        ///\n        ///     2) Some task-parallel implementations implement potentially\n        ///         cross-thread completions by callling\n        ///         SynchronizationContext.Post and Wait() and an event to be\n        ///         signaled.  If this was not a true cross-thread completion,\n        ///         but rather just two seperate instances of\n        ///         DispatcherSynchronizationContext for the same thread, this\n        ///         would result in a deadlock.\n        /// </summary>\n        public static bool ReuseDispatcherSynchronizationContextInstance\n        {\n            get { return _reuseDispatcherSynchronizationContextInstance; }\n            set\n            {\n                lock (_lockObject)\n                {\n                    if (_isSealed)\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.CompatibilityPreferencesSealed, \"ReuseDispatcherSynchronizationContextInstance\", \"BaseCompatibilityPreferences\"));\n                    }\n\n                    _reuseDispatcherSynchronizationContextInstance = value;\n                }\n            }\n        }\n\n        internal static bool GetReuseDispatcherSynchronizationContextInstance()\n        {\n            Seal();\n\n            return ReuseDispatcherSynchronizationContextInstance;\n        }\n\n#if NETFX && !NETCOREAPP\n        private static bool _reuseDispatcherSynchronizationContextInstance = BinaryCompatibility.TargetsAtLeast_Desktop_V4_5 ? false : true;\n#elif NETCOREAPP\n        private static bool _reuseDispatcherSynchronizationContextInstance = false;","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/BaseCompatibilityPreferences.cs#L66-L102","documentation":"BaseCompatibilityPreferences.ReuseDispatcherSynchronizationContextInstance can only be set before WPF seals the compatibility preferences (which happens once the Application/Dispatcher infrastructure initializes). Setting it afterwards throws InvalidOperationException with SR.CompatibilityPreferencesSealed naming this property. The setter is guarded by a lock and an _isSealed flag.","triggerScenarios":"Assigning BaseCompatibilityPreferences.ReuseDispatcherSynchronizationContextInstance = true/false anywhere after the Application has been constructed or a Dispatcher has started (e.g. in App.OnStartup after base init, or on a background thread late in app lifetime).","commonSituations":"Moving compatibility settings out of the static Main/PreStart phase during a refactor; setting them in a unit test after a previous test already touched WPF; setting them inside event handlers.","solutions":["Move the assignment to the earliest point in the process, e.g. before creating the Application object (in Main or a module initializer).","Ensure all BaseCompatibilityPreferences writes happen on one thread before any WPF type from PresentationFramework is touched.","In tests, run each preference-setting test in its own process/appdomain, or read rather than write preferences after init."],"exampleFix":"// before\nprotected override void OnStartup(StartupEventArgs e)\n{\n    BaseCompatibilityPreferences.ReuseDispatcherSynchronizationContextInstance = true;\n    ...\n}\n// after\npublic static void Main()\n{\n    BaseCompatibilityPreferences.ReuseDispatcherSynchronizationContextInstance = true;\n    var app = new App();\n    app.Run();\n}","handlingStrategy":"validation","validationCode":"// Call this in Main before any WPF type is used:\nvoid ConfigureCompatPreferences()\n{\n    // must be the first WPF touch point in the process\n    BaseCompatibilityPreferences.ReuseDispatcherSynchronizationContextInstance = true;\n}","typeGuard":null,"tryCatchPattern":"try { BaseCompatibilityPreferences.ReuseDispatcherSynchronizationContextInstance = v; }\ncatch (InvalidOperationException) { /* already sealed: setting ignored — log and continue */ }","preventionTips":["Set all BaseCompatibilityPreferences before constructing Application.","Keep them in one static pre-init location (Main or module initializer).","Never set them from event handlers, libraries, or tests after WPF init."],"tags":["wpf","invalid-state","dispatcher","initialization-order"],"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"}