{"record":{"id":"d7dd5043b28ba637","repo":"dotnet/wpf","slug":"sr-endinitwithoutbegininitnotsupported-frameworkelement","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/FrameworkElement.cs","lineNumber":5413,"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":5395,"sourceCodeEnd":5431,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs#L5395-L5431","documentation":"EndInit must be preceded by a matching BeginInit (ISupportInitialize contract). If EndInit is called when the InitPending flag is not set, WPF throws InvalidOperationException(SR.EndInitWithoutBeginInitNotSupported). This keeps the initialization lifecycle balanced so Initialized state and events fire correctly.","triggerScenarios":"Calling EndInit() on a FrameworkElement on which BeginInit() was never called (or whose pending flag was already cleared by a previous EndInit).","commonSituations":"Exception in the configure section causing BeginInit's pending state confusion; helper code that calls EndInit in finally without ensuring BeginInit ran; duplicate finally blocks calling EndInit twice.","solutions":["Always pair EndInit with a BeginInit in the same scope, ideally in a finally block","Do not call EndInit defensively on elements you did not BeginInit yourself","If an exception occurs between Begin/EndInit, still call EndInit exactly once or recreate the element","Use a boolean flag around the pair to guarantee 1:1 pairing"],"exampleFix":"// before\ntry { configure(el); } finally { el.EndInit(); } // BeginInit missing\n// after\nel.BeginInit();\ntry { configure(el); } finally { el.EndInit(); }","handlingStrategy":"validation","validationCode":"if (!el.HasInitPending) /* your own tracking */ throw new InvalidOperationException(\"EndInit without BeginInit\");","typeGuard":null,"tryCatchPattern":"try { el.EndInit(); }\ncatch (InvalidOperationException) { /* unbalanced EndInit; ignore or log */ }","preventionTips":["Guarantee 1:1 Begin/EndInit pairing with try/finally","Never call EndInit defensively on elements you did not begin","On exception between Begin/EndInit, still call EndInit exactly once"],"tags":["wpf","initialization","invalid-operation"],"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"}