dotnet/wpf · error · InvalidOperationException

SR.EndInitWithoutBeginInitNotSupported

Error message

SR.EndInitWithoutBeginInitNotSupported

What it means

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.

Solutions

  1. Always pair EndInit with a BeginInit in the same scope, ideally in a finally block
  2. Do not call EndInit defensively on elements you did not BeginInit yourself
  3. If an exception occurs between Begin/EndInit, still call EndInit exactly once or recreate the element
  4. Use a boolean flag around the pair to guarantee 1:1 pairing

Example fix

// before
try { configure(el); } finally { el.EndInit(); } // BeginInit missing
// after
el.BeginInit();
try { configure(el); } finally { el.EndInit(); }
Defensive patterns

Strategy: validation

Validate before calling

if (!el.HasInitPending) /* your own tracking */ throw new InvalidOperationException("EndInit without BeginInit");

Try / catch

try { el.EndInit(); }
catch (InvalidOperationException) { /* unbalanced EndInit; ignore or log */ }

Prevention

When it happens

Trigger: Calling EndInit() on a FrameworkElement on which BeginInit() was never called (or whose pending flag was already cleared by a previous EndInit).

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/d7dd5043b28ba637. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElement.cs:5413

            // Nested BeginInits on the same instance aren't permitted
            if (ReadInternalFlag(InternalFlags.InitPending))
            {
                throw new InvalidOperationException(SR.NestedBeginInitNotSupported);
            }

            // Mark the element as pending initialization
            WriteInternalFlag(InternalFlags.InitPending, true);
        }

        /// <summary>
        ///     Initialization of this element has completed
        /// </summary>
        public virtual void EndInit()
        {
            // Every EndInit must be preceeded by a BeginInit
            if (!ReadInternalFlag(InternalFlags.InitPending))
            {
                throw new InvalidOperationException(SR.EndInitWithoutBeginInitNotSupported);
            }

            // Reset the pending flag
            WriteInternalFlag(InternalFlags.InitPending, false);

            // Mark the element initialized and fire Initialized event
            // (eg. tree building via parser)
            TryFireInitialized();
        }

        /// <summary>
        ///     Has this element been initialized
        /// </summary>
        /// <remarks>
        ///     True if either EndInit or OnParentChanged were called
        /// </remarks>
        [EditorBrowsable(EditorBrowsableState.Advanced)]
        public bool IsInitialized

View on GitHub (pinned to 81131a70a4)