dotnet/wpf · error · InvalidOperationException

SR.OnlyOneInitialization

Error message

SR.OnlyOneInitialization

What it means

GlyphRun implements ISupportInitialize; BeginInit() throws InvalidOperationException(SR.OnlyOneInitialization) when the GlyphRun is already fully initialized. A GlyphRun's properties can only be set during one initialization batch; after EndInit (or when constructed complete), re-initialization is disallowed.

Solutions

  1. Create a new GlyphRun instance for each new set of properties instead of re-initializing an existing one.
  2. Check glyphRun.IsInitialized before calling BeginInit(); skip Begin/EndInit if already initialized.
  3. If using deferred initialization, ensure EndInit is called exactly once per BeginInit and never call Begin again afterward.

Example fix

// before
glyphRun.BeginInit(); // second pass on same instance
// after
var glyphRun = new GlyphRun();
glyphRun.BeginInit();
// set properties...
glyphRun.EndInit();
Defensive patterns

Strategy: validation

Validate before calling

if (glyphRun.IsInitialized) { glyphRun = new GlyphRun(); }
glyphRun.BeginInit();

Type guard

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

Try / catch

try { glyphRun.BeginInit(); }
catch (InvalidOperationException) { glyphRun = new GlyphRun(); glyphRun.BeginInit(); }

Prevention

When it happens

Trigger: Calling BeginInit() on a GlyphRun where IsInitialized is true — typically calling BeginInit a second time, or on a GlyphRun created via the full constructor after EndInit has completed.

Common situations: XAML/baml re-initialization paths, reusing a cached GlyphRun instance for new content through ISupportInitialize, or animation/tooling code that drives BeginInit/EndInit manually.

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/871254c8b27d20b8. Report an issue: GitHub.

Appendix: source

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

            {
                throw new NotSupportedException();
            }

            #endregion

            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);
            }

View on GitHub (pinned to 81131a70a4)