{"record":{"id":"240f516a8975bceb","repo":"dotnet/wpf","slug":"sr-arrangereentrancyinvalid-textflow","errorCode":null,"errorMessage":"SR.ArrangeReentrancyInvalid","messagePattern":"SR\\.ArrangeReentrancyInvalid","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextFlow.cs","lineNumber":1515,"sourceCode":"            if (uiChild != null)\n            {\n                OnChildUIElementChanged(uiChild);\n            }\n        }\n\n        // ------------------------------------------------------------------\n        // Ensures none of our methods can be called during measure/arrange/content change.\n        // ------------------------------------------------------------------\n        private void VerifyReentrancy()\n        {\n            if(CheckFlags(Flags.MeasureInProgress))\n            {\n                throw new InvalidOperationException(SR.MeasureReentrancyInvalid);\n            }\n\n            if(CheckFlags(Flags.ArrangeInProgress))\n            {\n                throw new InvalidOperationException(SR.ArrangeReentrancyInvalid);\n            }\n\n            if(CheckFlags(Flags.ContentChangeInProgress))\n            {\n                throw new InvalidOperationException(SR.TextContainerChangingReentrancyInvalid);\n            }\n        }\n\n        // ------------------------------------------------------------------\n        /// CalculateViewportRect - Called to calculate the visible rect for this element\n        // ------------------------------------------------------------------\n        private Rect CalculateViewportRect()\n        {\n            Rect visibleRect = new Rect(0, 0, RenderSize.Width, RenderSize.Height);\n\n            Visual visual = VisualTreeHelper.GetParent(this) as Visual;\n\n            while(visual != null && visibleRect != Rect.Empty)","sourceCodeStart":1497,"sourceCodeEnd":1533,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextFlow.cs#L1497-L1533","documentation":"Same reentrancy guard as MeasureReentrancyInvalid, but for the arrange phase: VerifyReentrancy throws InvalidOperationException(SR.ArrangeReentrancyInvalid) when a public TextFlow method is called while Flags.ArrangeInProgress is set. TextFlow forbids any public API entry during arrange.","triggerScenarios":"Calling any public TextFlow method from ArrangeOverride, from ArrangeCore overrides, or from callbacks raised synchronously during arrange (e.g. property invalidation handlers triggered mid-arrange).","commonSituations":"Custom panels/text hosts that adjust text layout inside OnArrange/ArrangeOverride; handlers that mutate the document or query positions in response to size changes computed during arrange.","solutions":["Defer the TextFlow call until after arrange completes (Dispatcher.BeginInvoke at Background priority).","Perform document/position changes before initiating layout, not inside ArrangeOverride.","Queue invalidation (InvalidateMeasure/InvalidateArrange) rather than calling TextFlow directly during arrange."],"exampleFix":"// before\nprotected override Size ArrangeOverride(Size finalSize) {\n    _textFlow.SetTextContainerDefaults(...); // reentrancy\n    return base.ArrangeOverride(finalSize);\n}\n// after\nprotected override Size ArrangeOverride(Size finalSize) {\n    Dispatcher.BeginInvoke(() => _textFlow.SetTextContainerDefaults(...));\n    return base.ArrangeOverride(finalSize);\n}","handlingStrategy":"try-catch","validationCode":"// Cannot pre-check private flags; avoid TextFlow calls inside ArrangeOverride.","typeGuard":null,"tryCatchPattern":"try { DoTextFlowWork(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"arrange\")) { Dispatcher.BeginInvoke(DoTextFlowWork); }","preventionTips":["Keep ArrangeOverride free of TextFlow/document mutations","Use InvalidateMeasure/InvalidateArrange to request later work instead of calling in-line","Defer event-driven layout work to the dispatcher queue"],"tags":["wpf","reentrancy","layout"],"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"}