{"record":{"id":"b73883496e740e3a","repo":"dotnet/wpf","slug":"sr-ininitialization-glyphtypeface","errorCode":null,"errorMessage":"SR.InInitialization","messagePattern":"SR\\.InInitialization","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs","lineNumber":1656,"sourceCode":"            }\n        }\n \n        #endregion Private Methods\n\n        #region ISupportInitialize interface\n\n        void ISupportInitialize.BeginInit()\n        {\n            if (_initializationState == InitializationState.IsInitialized)\n            {\n                // Cannot initialize a GlyphRun this is completely initialized.\n                throw new InvalidOperationException(SR.OnlyOneInitialization);\n            }\n\n            if (_initializationState == InitializationState.IsInitializing)\n            {\n                // Cannot initialize a GlyphRun this already being initialized.\n                throw new InvalidOperationException(SR.InInitialization);\n            }\n\n            _initializationState = InitializationState.IsInitializing;\n        }\n\n        void ISupportInitialize.EndInit()\n        {\n            if (_initializationState != InitializationState.IsInitializing)\n            {\n                // Cannot EndInit a GlyphRun that is not being initialized.\n                throw new InvalidOperationException(SR.NotInInitialization);\n            }\n\n            Initialize(_originalUri, _styleSimulations);\n        }\n\n        private void CheckInitialized()\n        {","sourceCodeStart":1638,"sourceCodeEnd":1674,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphTypeface.cs#L1638-L1674","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nface.BeginInit();\nface.BeginInit(); // throws InInitialization\nface.EndInit();\n\n// after\nface.BeginInit();\nface.UriSource = fontUri;\nface.EndInit(); // single Begin/EndInit pair per cycle","handlingStrategy":"validation","validationCode":"// Guard wrapper: only BeginInit when not already initializing/initialized\nstatic void SafeBeginInit(GlyphTypeface face) { if (((ISupportInitialize)face).IsInitialized) throw new InvalidOperationException(\"Already fully initialized\"); /* track an isInitializing flag in your wrapper to prevent nesting */ }","typeGuard":"static bool IsMidInitialization(bool beginCalled, bool endCalled) => beginCalled && !endCalled;","tryCatchPattern":"try { ((ISupportInitialize)face).BeginInit(); }\ncatch (InvalidOperationException) { /* a BeginInit cycle is already open; skip or recreate */ }","preventionTips":["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."],"tags":["wpf","glyphrun","ispupportinitialize","initialization-state","invalid-operation"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}