{"record":{"id":"987cfd58807ae1f6","repo":"dotnet/wpf","slug":"sr-nestedbegininitnotsupported-resourcedictionary","errorCode":null,"errorMessage":"SR.NestedBeginInitNotSupported","messagePattern":"SR\\.NestedBeginInitNotSupported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs","lineNumber":992,"sourceCode":"        }\n\n        #endregion IEnumerable\n\n        #region ISupportInitialize\n\n        /// <summary>\n        ///     Mark the begining of the Init phase\n        /// </summary>\n        /// <remarks>\n        ///     BeginInit and EndInit follow a transaction model. BeginInit marks the\n        ///     dictionary uninitialized and EndInit marks it initialized.\n        /// </remarks>\n        public void BeginInit()\n        {\n            // Nested BeginInits on the same instance aren't permitted\n            if (IsInitializePending)\n            {\n                throw new InvalidOperationException(SR.NestedBeginInitNotSupported);\n            }\n\n            IsInitializePending = true;\n            IsInitialized = false;\n        }\n\n        /// <summary>\n        ///     Fire Invalidation at the end of Init phase\n        /// </summary>\n        /// <remarks>\n        ///     BeginInit and EndInit follow a transaction model. BeginInit marks the\n        ///     dictionary uninitialized and EndInit marks it initialized.\n        /// </remarks>\n        public void EndInit()\n        {\n            // EndInit without a BeginInit isn't permitted\n            if (!IsInitializePending)\n            {","sourceCodeStart":974,"sourceCodeEnd":1010,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs#L974-L1010","documentation":"ResourceDictionary.BeginInit throws InvalidOperationException (SR.NestedBeginInitNotSupported) when called while another initialization is already pending (IsInitializePending). Nested Begin/EndInit pairs on the same instance are not permitted.","triggerScenarios":"Calling BeginInit twice on the same ResourceDictionary without a matching EndInit in between — e.g. nested load helpers or re-entrant initialization code.","commonSituations":"Two theme-loading routines both calling BeginInit on the same shared dictionary; exception in the middle of a Begin/EndInit block leaving IsInitializePending stuck, then retrying initialization.","solutions":["Ensure every BeginInit is paired with exactly one EndInit, including on exception paths (try/finally).","Check IsInitializePending before calling BeginInit, or guard with a bool flag in your own code.","If an earlier EndInit was skipped due to an exception, restructure to use try/finally so the state resets."],"exampleFix":"// before\ndictionary.BeginInit();\nLoadThemes(dictionary);\ndictionary.BeginInit(); // nested -> throws\n// after\ndictionary.BeginInit();\ntry { LoadThemes(dictionary); }\nfinally { dictionary.EndInit(); }","handlingStrategy":"try-catch","validationCode":"if (dictionary.IsInitializePending) throw new InvalidOperationException(\"BeginInit already called; call EndInit before initializing again\");","typeGuard":"bool CanBeginInit(ResourceDictionary rd) => !rd.IsInitializePending;","tryCatchPattern":"try { dictionary.BeginInit(); /* configure */ } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Nested BeginInit\")) { /* an init is already in flight; join it or wait */ }","preventionTips":["Always pair BeginInit/EndInit in a try/finally","Track initialization state with an application-level flag for shared dictionaries","Never call BeginInit from re-entrant or nested load helpers on the same instance"],"tags":["wpf","resourcedictionary","initialization"],"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-22T01:17:13.364Z"}