{"record":{"id":"b9553bd99cb8f217","repo":"dotnet/wpf","slug":"sr-endinitwithoutbegininitnotsupported","errorCode":null,"errorMessage":"SR.EndInitWithoutBeginInitNotSupported","messagePattern":"SR\\.EndInitWithoutBeginInitNotSupported","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkContentElement.cs","lineNumber":1544,"sourceCode":"            // Nested BeginInits on the same instance aren't permitted\n            if (ReadInternalFlag(InternalFlags.InitPending))\n            {\n                throw new InvalidOperationException(SR.NestedBeginInitNotSupported);\n            }\n\n            // Mark the element as pending initialization\n            WriteInternalFlag(InternalFlags.InitPending, true);\n        }\n\n        /// <summary>\n        ///     Initialization of this element has completed\n        /// </summary>\n        public virtual void EndInit()\n        {\n            // Every EndInit must be preceeded by a BeginInit\n            if (!ReadInternalFlag(InternalFlags.InitPending))\n            {\n                throw new InvalidOperationException(SR.EndInitWithoutBeginInitNotSupported);\n            }\n\n            // Reset the pending flag\n            WriteInternalFlag(InternalFlags.InitPending, false);\n\n            // Mark the element initialized and fire Initialized event\n            // (eg. tree building via parser)\n            TryFireInitialized();\n        }\n\n        /// <summary>\n        ///     Has this element been initialized\n        /// </summary>\n        /// <remarks>\n        ///     True if either EndInit or OnParentChanged were called\n        /// </remarks>\n        [EditorBrowsable(EditorBrowsableState.Advanced)]\n        public bool IsInitialized","sourceCodeStart":1526,"sourceCodeEnd":1562,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkContentElement.cs#L1526-L1562","documentation":"FrameworkContentElement.EndInit throws InvalidOperationException if called without a preceding BeginInit. EndInit completes the initialization phase, marks the element as initialized, and raises the Initialized event; calling it with no pending init would produce spurious Initialized events and break the BeginInit/EndInit contract.","triggerScenarios":"Calling EndInit() on a FrameworkContentElement whose InitPending flag is false - i.e., no BeginInit was called, or EndInit was already called once for the current BeginInit.","commonSituations":"Double-EndInit in error-prone factory code (EndInit in both a catch block and normal path); calling EndInit on elements that the XAML loader initializes internally.","solutions":["Pair BeginInit/EndInit one-to-one using try/finally so EndInit runs exactly once per BeginInit","Track the pending state yourself (or query element initialization state) before calling EndInit","Remove redundant EndInit calls when the framework/XAML loader already finalizes the element"],"exampleFix":"// before\nel.EndInit();\nConfigure(el);\nel.EndInit(); // second call throws\n\n// after\nel.BeginInit();\ntry { Configure(el); }\nfinally { el.EndInit(); }","handlingStrategy":"validation","validationCode":"if (!initPending) // must have called BeginInit first\n    el.BeginInit();\ntry { Configure(el); }\nfinally { el.EndInit(); }","typeGuard":"bool IsEndInitAllowed => initPending;","tryCatchPattern":"try { el.EndInit(); }\ncatch (InvalidOperationException ex) { log.Error(\"EndInit without BeginInit\", ex); }","preventionTips":["Enforce one-to-one BeginInit/EndInit pairing","Put EndInit in a finally block so it runs exactly once","Skip manual EndInit for elements initialized by the XAML loader"],"tags":["wpf","initialization","invalid-state-transition"],"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"}