{"record":{"id":"16c8993fb5623462","repo":"dotnet/wpf","slug":"sr-measurereentrancyinvalid-textflow","errorCode":null,"errorMessage":"SR.MeasureReentrancyInvalid","messagePattern":"SR\\.MeasureReentrancyInvalid","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextFlow.cs","lineNumber":1510,"sourceCode":"        {\n            // One of UIElement children has been changed. Need to \n            // notify NameTable about the change and remeasure content.\n            // Treat this notification as OnChildUIElementChanged.\n            UIElement uiChild = source as UIElement;\n            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        {","sourceCodeStart":1492,"sourceCodeEnd":1528,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextFlow.cs#L1492-L1528","documentation":"TextFlow guards against reentrancy during its measure pass. VerifyReentrancy checks the MeasureInProgress flag and throws InvalidOperationException(SR.MeasureReentrancyInvalid) if any public TextFlow method is invoked while measure is running. Text layout must not be queried or mutated from inside measure.","triggerScenarios":"Calling a public TextFlow method (e.g. formatting, TextContainer changes, position queries) from within MeasureOverride of a TextFlow-driven control, or from a layout event/callback raised synchronously during measure.","commonSituations":"Custom text host controls that query text positions inside OnMeasure/MeasureOverride; event handlers (e.g. TextContainer change handlers) that trigger re-layout or formatting calls while measure is already in progress.","solutions":["Move the TextFlow call out of the measure path — defer with Dispatcher.BeginInvoke or perform it after layout completes.","Cache the values you need before measure starts instead of querying TextFlow during measure.","If responding to a content-changed event, defer the response until the ContentChange scope (and measure) finishes."],"exampleFix":"// before\nprotected override Size MeasureOverride(Size constraint) {\n    var size = _textFlow.CalculateElementSize(constraint);\n    var r = _textFlow.GetRectangles(_element); // reentrancy\n    return size;\n}\n// after\nprotected override Size MeasureOverride(Size constraint) {\n    var size = _textFlow.CalculateElementSize(constraint);\n    Dispatcher.BeginInvoke(() => RenderRectangles(_textFlow.GetRectangles(_element)));\n    return size;\n}","handlingStrategy":"try-catch","validationCode":"// Cannot pre-check private flags; instead restructure so TextFlow is not called during MeasureOverride.","typeGuard":null,"tryCatchPattern":"try { DoTextFlowWork(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"measure\")) { Dispatcher.BeginInvoke(DoTextFlowWork); }","preventionTips":["Never call TextFlow public APIs from MeasureOverride","Defer work with Dispatcher.BeginInvoke when responding to events fired during measure","Cache needed values before layout runs"],"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-22T01:17:13.364Z"}