dotnet/wpf · error · InvalidOperationException

SR.InInitialization

Error message

SR.InInitialization

What it means

BeginInit() throws InvalidOperationException(SR.InInitialization) when BeginInit is called while a previous initialization batch is still open (IsInitializing is true). Initialization batches must not be nested: exactly one BeginInit/EndInit cycle at a time.

Solutions

  1. Pair every BeginInit with EndInit in a try/finally so an exception cannot leave the GlyphRun stuck in the initializing state.
  2. Check glyphRun.IsInitializing before calling BeginInit() and skip or end the prior batch first.
  3. Ensure only one code path drives ISupportInitialize for a given GlyphRun instance.

Example fix

// before
glyphRun.BeginInit();
InitPart1(glyphRun);
glyphRun.BeginInit(); // nested: throws
// after
glyphRun.BeginInit();
try { InitPart1(glyphRun); InitPart2(glyphRun); }
finally { glyphRun.EndInit(); }
Defensive patterns

Strategy: try-catch

Validate before calling

if (!glyphRun.IsInitializing) glyphRun.BeginInit();

Type guard

bool CanBeginInit(GlyphRun g) => !g.IsInitializing;

Try / catch

try { glyphRun.BeginInit(); /* set props */ glyphRun.EndInit(); }
finally { if (glyphRun.IsInitializing) glyphRun.EndInit(); }

Prevention

When it happens

Trigger: Calling BeginInit() twice without an intervening EndInit() — e.g. nested setup code, an exception skipping EndInit, or two code paths both driving ISupportInitialize on the same GlyphRun.

Common situations: Exception in the middle of an init block leaves IsInitializing stuck true; a retry then throws. Also occurs when XAML loader and custom code both call BeginInit on the same element.

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/3fee90a82b69905c. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs:2297

            private int _count;
        }

        #endregion Hit testing

        #region ISupportInitialize interface for Xaml serialization

        void ISupportInitialize.BeginInit()
        {
            if (IsInitialized)
            {
                // Cannot initialize a GlyphRun that is completely initialized.
                throw new InvalidOperationException(SR.OnlyOneInitialization);
            }

            if (IsInitializing)
            {
                // Cannot initialize a GlyphRun that is already being initialized.
                throw new InvalidOperationException(SR.InInitialization);
            }

            IsInitializing = true;
        }

        void ISupportInitialize.EndInit()
        {
            if (!IsInitializing)
            {
                // Cannot EndInit a GlyphRun that is not being initialized.
                throw new InvalidOperationException(SR.NotInInitialization);
            }

            //
            // Fully initilize the GlyphRun. The method will check for consistency
            // between all the properties.
            //
            Initialize(

View on GitHub (pinned to 81131a70a4)