question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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");
}

ScreenCapture_6-2-2022 4 50 39 PM

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
miloushcommented, Jun 3, 2022

@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?

1reaction
miloushcommented, Jun 9, 2022

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. With Render (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 use Task:

var typef = new Typeface(FontFamily, new FontStyle(), FontWeights.Normal, FontStretch);
Task.Run(() =>
{
    for (int i = 0; i <= 30000; i++)
    {
        FormattedText text = new FormattedText("Demo", System.Globalization.CultureInfo.CurrentCulture, FlowDirection.LeftToRight, typef, 14, Brushes.Black);
        Size demo = new Size(text.Width, text.Height);
    }
    MessageBox.Show("Initialized");
});

(Caveat is obviously you can’t touch any objects that require Dispatcher from the other thread.)

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found