dotnet/wpf · error · InvalidOperationException
SR.InInitialization
Error message
SR.InInitialization
What it means
GlyphTypeface's ISupportInitialize.BeginInit throws SR.InInitialization when called while the GlyphTypeface is already mid-initialization (_initializationState == InitializationState.IsInitializing). A BeginInit/EndInit cycle cannot be nested or restarted before EndInit completes, so a second BeginInit during an open cycle is rejected with InvalidOperationException.
Solutions
- Ensure BeginInit is called exactly once per cycle, followed by EndInit before any further property setup.
- Wrap the EndInit call in try/finally so a failed EndInit does not leave the instance in the IsInitializing state.
- Discard the half-initialized instance and construct a new GlyphTypeface instead of re-calling BeginInit.
Example fix
// before face.BeginInit(); face.BeginInit(); // throws InInitialization face.EndInit(); // after face.BeginInit(); face.UriSource = fontUri; face.EndInit(); // single Begin/EndInit pair per cycle
Defensive patterns
Strategy: validation
Validate before calling
// Guard wrapper: only BeginInit when not already initializing/initialized
static void SafeBeginInit(GlyphTypeface face) { if (((ISupportInitialize)face).IsInitialized) throw new InvalidOperationException("Already fully initialized"); /* track an isInitializing flag in your wrapper to prevent nesting */ } Type guard
static bool IsMidInitialization(bool beginCalled, bool endCalled) => beginCalled && !endCalled;
Try / catch
try { ((ISupportInitialize)face).BeginInit(); }
catch (InvalidOperationException) { /* a BeginInit cycle is already open; skip or recreate */ } Prevention
- Never call BeginInit from two layers of code (wrapper + caller) on the same instance.
- Keep Begin/EndInit calls adjacent in the same method so nesting cannot occur.
- Log/unwrap designer-generated code that may emit duplicate BeginInit calls.
When it happens
Trigger: Calling ISupportInitialize.BeginInit() twice in a row without a successful EndInit() in between; nested BeginInit calls from wrapper code that calls BeginInit both in a setup helper and again inline.
Common situations: Code generated by designers that emits an extra BeginInit; exception in EndInit leaves the instance in IsInitializing state and recovery code calls BeginInit again; wrapper libraries that call BeginInit defensively before delegating to user code that also calls it.
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
- SR.NotInInitialization
- SR.OnlyOneInitialization
- SR.InitializationIncomplete
- SR.InInitialization
- SR.NotInInitialization
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/b73883496e740e3a.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs:1656
}
}
#endregion Private Methods
#region ISupportInitialize interface
void ISupportInitialize.BeginInit()
{
if (_initializationState == InitializationState.IsInitialized)
{
// 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()
{View on GitHub (pinned to 81131a70a4)