{"record":{"id":"c9b0ae85707e4b06","repo":"dotnet/wpf","slug":"sr-image-ininitialize","errorCode":null,"errorMessage":"SR.Image_InInitialize","messagePattern":"SR\\.Image_InInitialize","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs","lineNumber":24,"sourceCode":"namespace System.Windows.Media.Imaging\n{\n    #region BitmapInitialize\n    /// <summary>\n    /// Utility class providing support for ISupportInitialize\n    /// </summary>\n    internal class BitmapInitialize : ISupportInitialize\n    {\n        public BitmapInitialize()\n        {\n        }\n\n        public void BeginInit()\n        {\n            if (IsInitAtLeastOnce)\n                throw new InvalidOperationException(SR.Format(SR.Image_OnlyOneInit, null));\n\n            if (IsInInit)\n                throw new InvalidOperationException(SR.Format(SR.Image_InInitialize, null));\n\n            _inInit = true;\n        }\n\n        public void EndInit()\n        {\n            if (!IsInInit)\n                throw new InvalidOperationException(SR.Format(SR.Image_EndInitWithoutBeginInit, null));\n\n            _inInit = false;\n            _isInitialized = true;\n        }\n\n        public void SetPrologue()\n        {\n            if (!IsInInit)\n            {\n                throw new InvalidOperationException(SR.Format(SR.Image_SetPropertyOutsideBeginEndInit, null));","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/BitmapInitialize.cs#L6-L42","documentation":"BeginInit sets the _inInit flag; calling BeginInit again while an initialization block is already open means a previous BeginInit was never paired with EndInit. WPF throws InvalidOperationException with SR.Image_InInitialize to prevent nested initialization.","triggerScenarios":"Calling BeginInit() while IsInInit is true — i.e. a second BeginInit before the matching EndInit, typically from a helper method called inside an already-open BeginInit/EndInit block.","commonSituations":"A shared helper that calls BeginInit itself while the caller already called BeginInit; exception in the middle of init skipped EndInit, then retry logic calls BeginInit again; event handlers or async callbacks re-entering init code.","solutions":["Remove the redundant BeginInit call — only call it once before setting properties","Ensure EndInit is called in a finally block so a failed init resets state, and create a new instance for retries","Refactor helpers to accept an already-initializing object instead of calling BeginInit themselves"],"exampleFix":"// before\nimg.BeginInit();\nConfigure(img); // helper also calls img.BeginInit() -> throws\nimg.EndInit();\n// after\nimg.BeginInit();\nConfigure(img); // helper only sets properties (UriSource, etc.)\nimg.EndInit();","handlingStrategy":"validation","validationCode":"bool canEndInitSafely(BitmapImage img, ref bool begun) => begun == false; // call BeginInit only when no init block is open","typeGuard":null,"tryCatchPattern":"try { img.BeginInit(); /* props */; img.EndInit(); }\ncatch (InvalidOperationException ex)\n{\n    // nested init: init block already open\n    logger.LogError(ex, \"Nested BeginInit on BitmapImage\");\n}","preventionTips":["Call BeginInit exactly once per init block, never inside helper methods that the caller also uses within the block","Ensure helpers only set properties, not BeginInit","Use try/finally so EndInit always runs after BeginInit, preventing leaked init state"],"tags":["wpf","initialization","nested-init","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"}