dotnet/wpf · error · InvalidOperationException

SR.NotInInitialization

Error message

SR.NotInInitialization

What it means

GlyphTypeface's ISupportInitialize.EndInit throws SR.NotInInitialization when EndInit is called while _initializationState is not IsInitializing, i.e. when no BeginInit cycle is open. EndInit is only valid as the closing half of a BeginInit/EndInit pair, otherwise it throws InvalidOperationException. The same guard backs private CheckInitializing (error 1154).

Solutions

  1. Call BeginInit before EndInit so the pair is balanced.
  2. Only call EndInit in a finally block when the matching BeginInit actually succeeded (use a boolean flag).
  3. Remove redundant EndInit calls; a completed GlyphTypeface needs no further EndInit.

Example fix

// before
try
{
    ((ISupportInitialize)face).BeginInit();
}
finally
{
    ((ISupportInitialize)face).EndInit(); // throws if BeginInit threw
}

// after
var inited = false;
try
{
    ((ISupportInitialize)face).BeginInit();
    inited = true;
}
finally
{
    if (inited) ((ISupportInitialize)face).EndInit();
}
Defensive patterns

Strategy: validation

Validate before calling

// Track your own cycle state so EndInit is only called after a successful BeginInit
bool beganInit = false;
((ISupportInitialize)face).BeginInit(); beganInit = true;
if (beganInit) ((ISupportInitialize)face).EndInit();

Type guard

static bool CanEndInit(bool beginSucceeded, bool endAlreadyCalled) => beginSucceeded && !endAlreadyCalled;

Try / catch

try { ((ISupportInitialize)face).EndInit(); }
catch (InvalidOperationException) { /* no open initialization cycle; ignore or fail fast in tests */ }

Prevention

When it happens

Trigger: Calling ISupportInitialize.EndInit() on a fresh GlyphTypeface before any BeginInit; calling EndInit twice; calling EndInit after initialization already completed (state IsInitialized).

Common situations: Cleanup/finally blocks that call EndInit unconditionally even when BeginInit failed; generated designer code with mismatched Begin/EndInit emission; copy-pasted two-phase initialization code where BeginInit was removed or commented out.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs:1667

                // Cannot initialize a GlyphRun this is completely initialized.
                throw new InvalidOperationException(SR.OnlyOneInitialization);
            }

            if (_initializationState == InitializationState.IsInitializing)
            {
                // Cannot initialize a GlyphRun this already being initialized.
                throw new InvalidOperationException(SR.InInitialization);
            }

            _initializationState = InitializationState.IsInitializing;
        }

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

            Initialize(_originalUri, _styleSimulations);
        }

        private void CheckInitialized()
        {
            if (_initializationState != InitializationState.IsInitialized)
            {
                throw new InvalidOperationException(SR.InitializationIncomplete);
            }
        }

        private void CheckInitializing()
        {
            if (_initializationState != InitializationState.IsInitializing)
            {
                throw new InvalidOperationException(SR.NotInInitialization);

View on GitHub (pinned to 81131a70a4)