dotnet/wpf · error · InvalidOperationException
SR.NestedBeginInitNotSupported
Error message
SR.NestedBeginInitNotSupported
What it means
ResourceDictionary.BeginInit throws InvalidOperationException (SR.NestedBeginInitNotSupported) when called while another initialization is already pending (IsInitializePending). Nested Begin/EndInit pairs on the same instance are not permitted.
Solutions
- Ensure every BeginInit is paired with exactly one EndInit, including on exception paths (try/finally).
- Check IsInitializePending before calling BeginInit, or guard with a bool flag in your own code.
- If an earlier EndInit was skipped due to an exception, restructure to use try/finally so the state resets.
Example fix
// before
dictionary.BeginInit();
LoadThemes(dictionary);
dictionary.BeginInit(); // nested -> throws
// after
dictionary.BeginInit();
try { LoadThemes(dictionary); }
finally { dictionary.EndInit(); } Defensive patterns
Strategy: try-catch
Validate before calling
if (dictionary.IsInitializePending) throw new InvalidOperationException("BeginInit already called; call EndInit before initializing again"); Type guard
bool CanBeginInit(ResourceDictionary rd) => !rd.IsInitializePending;
Try / catch
try { dictionary.BeginInit(); /* configure */ } catch (InvalidOperationException ex) when (ex.Message.Contains("Nested BeginInit")) { /* an init is already in flight; join it or wait */ } Prevention
- Always pair BeginInit/EndInit in a try/finally
- Track initialization state with an application-level flag for shared dictionaries
- Never call BeginInit from re-entrant or nested load helpers on the same instance
When it happens
Trigger: Calling BeginInit twice on the same ResourceDictionary without a matching EndInit in between — e.g. nested load helpers or re-entrant initialization code.
Common situations: Two theme-loading routines both calling BeginInit on the same shared dictionary; exception in the middle of a Begin/EndInit block leaving IsInitializePending stuck, then retrying initialization.
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.EndInitWithoutBeginInitNotSupported
- Cannot initialize compressor.
- Image_NeitherArgument (UriSource, StreamSource)
- SR.EndInitWithoutBeginInitNotSupported
- SR.EndInitWithoutBeginInitNotSupported
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/987cfd58807ae1f6.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/ResourceDictionary.cs:992
}
#endregion IEnumerable
#region ISupportInitialize
/// <summary>
/// Mark the begining of the Init phase
/// </summary>
/// <remarks>
/// BeginInit and EndInit follow a transaction model. BeginInit marks the
/// dictionary uninitialized and EndInit marks it initialized.
/// </remarks>
public void BeginInit()
{
// Nested BeginInits on the same instance aren't permitted
if (IsInitializePending)
{
throw new InvalidOperationException(SR.NestedBeginInitNotSupported);
}
IsInitializePending = true;
IsInitialized = false;
}
/// <summary>
/// Fire Invalidation at the end of Init phase
/// </summary>
/// <remarks>
/// BeginInit and EndInit follow a transaction model. BeginInit marks the
/// dictionary uninitialized and EndInit marks it initialized.
/// </remarks>
public void EndInit()
{
// EndInit without a BeginInit isn't permitted
if (!IsInitializePending)
{View on GitHub (pinned to 81131a70a4)