dotnet/wpf · error · InvalidOperationException

SR.DrawingGroup_AlreadyOpen

Error message

SR.DrawingGroup_AlreadyOpen

What it means

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.

Solutions

  1. Close the current session (end the using block / call Close) before calling Open again
  2. Use one Open session and call drawingContext methods inside it instead of Append during an open session
  3. Create a new DrawingGroup per open/close cycle rather than reusing one instance
  4. Guard re-entrant code with an _isOpen flag before opening

Example fix

// before
using (var ctx = group.Open())
{
    using (var ctx2 = group.Open()) { } // throws: already open
}

// after
using (var ctx = group.Open())
{
    ctx.DrawEllipse(...); // draw within the single open session
}
Defensive patterns

Strategy: validation

Validate before calling

if (groupOpen) throw new InvalidOperationException("Group is already open");
groupOpen = true;
try { using (var ctx = group.Open()) { /* draw */ } }
finally { groupOpen = false; }

Try / catch

try { using (var ctx = group.Open()) { /* draw */ } }
catch (InvalidOperationException ex) when (ex.Message.Contains("already"))
{
    // close the prior session or allocate a new DrawingGroup
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/b91c3f22db3adae2. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/DrawingGroup.cs:287

        }

         
        #endregion Internal methods     

        #region Private Methods

        /// <summary>
        /// Called by both Open() and Append(), this method verifies the
        /// DrawingGroup isn't already open, and set's the open flag.
        /// </summary>
        private void VerifyOpen()
        {
            WritePreamble();
            
            // Throw an exception if we are already opened
            if (_open)
            {
                throw new InvalidOperationException(SR.DrawingGroup_AlreadyOpen);                                
            }
            
            _open = true;
        }

        #endregion Private Methods        

        #region Private fields
        
        private bool _openedForAppend;
        private bool _open;
        #endregion Private fields        
    }
}

View on GitHub (pinned to 81131a70a4)