Delay occurs while getting the size from the FormattedText
See original GitHub issue- .NET Core Version: .Net 6.0
- Windows version: Windows11- 22000.675
- Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
- Is this bug related specifically to tooling in Visual Studio -No
Problem description: Delay Occurs while getting the size from the Formatted Text.
Expected behavior: Delay should not be occured. Minimal repro: 1.Run the sample 2.Click the Initialize Formatted Text button.
Note : Delay occurs, button is in pressed state for a long time. Please refer the below code snippet and gif file.
private void Button_Click(object sender, RoutedEventArgs e)
{
for (int i = 0; i <= 30000; i++)
{
FormattedText text = new FormattedText("Demo", System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, new Typeface(FontFamily, new FontStyle(), FontWeights.Normal, FontStretch), 14, Brushes.Black);
Size demo = new Size(text.Width, text.Height);
}
MessageBox.Show("Initialized");
}
Issue Analytics
- State:
- Created a year ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
WPF FormattedText "The system cannot find the file ...
The text retrieved needs to be in a specified canvas size. The service runs the code every 10 seconds and uses up to...
Read more >Excel for Mac (16.14.1) is very slow with text wrap
We are using an Excel file, approx. 40kB file size. Each cell contains several lines of formatted text, with text wrap on. While...
Read more >Cutting and Pasting Formatted Text
I'm wondering two things. 1, is there a way to cut text without havnig to select it first (which seems to be a...
Read more >Problems displaying formatted text
When the form is displayed(long after the Text property is set on the FormattedTextEditor control), the app displays a big red X over...
Read more >Issue using formatted text in “product short description” box
Just recently, everything was working fine, but as I was working on products this morning I started getting 500 errors. I turned on...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@sethuramkumar I think this issue needs a bit more information. Is the performance affecting a scenario in your application? Why would 30000 iterations be too few or too many, how many iterations would be reasonable to take the time it is taking now?
How are you assessing the delay? If I press the button using spacebar, with mouse away from it, and use
Normal
, the button stays blue while processing. WithRender
(or lower), it gets gray and then starts processing. If you use mouse, the button is blue on hover, the processing starts and then if you move mouse away, that enqueues another render, which will not execute until your processing is done (regardless of its priority).Either way, dispatcher can either be rendering or doing other things.
If you want to do stuff simultaneously, you will have to either need to be pre-emptive and make sure you give dispatcher chance to process other operations every now and then, or, you need a completely separate thread like @ygra suggested. Good news for you is that you don’t need a dispatcher for measuring
FormattedText
, so separate thread works fine. If it is just for a short task, you can useTask
:(Caveat is obviously you can’t touch any objects that require Dispatcher from the other thread.)