{"record":{"id":"b91c3f22db3adae2","repo":"dotnet/wpf","slug":"sr-drawinggroup-alreadyopen","errorCode":null,"errorMessage":"SR.DrawingGroup_AlreadyOpen","messagePattern":"SR\\.DrawingGroup_AlreadyOpen","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/DrawingGroup.cs","lineNumber":287,"sourceCode":"        }\n\n         \n        #endregion Internal methods     \n\n        #region Private Methods\n\n        /// <summary>\n        /// Called by both Open() and Append(), this method verifies the\n        /// DrawingGroup isn't already open, and set's the open flag.\n        /// </summary>\n        private void VerifyOpen()\n        {\n            WritePreamble();\n            \n            // Throw an exception if we are already opened\n            if (_open)\n            {\n                throw new InvalidOperationException(SR.DrawingGroup_AlreadyOpen);                                \n            }\n            \n            _open = true;\n        }\n\n        #endregion Private Methods        \n\n        #region Private fields\n        \n        private bool _openedForAppend;\n        private bool _open;\n        #endregion Private fields        \n    }\n}\n\n","sourceCodeStart":269,"sourceCodeEnd":303,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/DrawingGroup.cs#L269-L303","documentation":"DrawingGroup.VerifyOpen() threw because the group is already open (_open == true). A DrawingGroup supports only one Open at a time; calling Open() or Append() while a previous open session is still active raises InvalidOperationException with SR.DrawingGroup_AlreadyOpen.","triggerScenarios":"Calling group.Open() twice without an intervening Close(); calling Append() after Open() (Append manages its own open/close cycle); re-entrancy where an event handler opened the same group during an active Open.","commonSituations":"Nested 'using (group.Open())' blocks on the same instance; rendering callbacks that reopen a shared group each frame while the previous session is live; forgetting that Append itself opens and closes the group.","solutions":["Close the current session (end the using block / call Close) before calling Open again","Use one Open session and call drawingContext methods inside it instead of Append during an open session","Create a new DrawingGroup per open/close cycle rather than reusing one instance","Guard re-entrant code with an _isOpen flag before opening"],"exampleFix":"// before\nusing (var ctx = group.Open())\n{\n    using (var ctx2 = group.Open()) { } // throws: already open\n}\n\n// after\nusing (var ctx = group.Open())\n{\n    ctx.DrawEllipse(...); // draw within the single open session\n}","handlingStrategy":"validation","validationCode":"if (groupOpen) throw new InvalidOperationException(\"Group is already open\");\ngroupOpen = true;\ntry { using (var ctx = group.Open()) { /* draw */ } }\nfinally { groupOpen = false; }","typeGuard":null,"tryCatchPattern":"try { using (var ctx = group.Open()) { /* draw */ } }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"already\"))\n{\n    // close the prior session or allocate a new DrawingGroup\n}","preventionTips":["Use exactly one using(Open()) block per DrawingGroup instance","Do not call Append() while a session is open","Allocate a fresh DrawingGroup per render cycle instead of reusing"],"tags":["wpf","drawinggroup","reentrancy","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"}