• 12
• 12
• 9
• 10
• 13

[.net] Updating multiple Windows.Forms.Labels very slow

This topic is 3814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi there, As part of a game when I hover the mouse over a unit I show that units stats. To display the stats I alter the text of one label per attribute. The problem is this is really slowing my programme down when I am doing other graphical stuff at the same time as updating the stats. Below is an example.
In the code below all the updated items are labels apart from the last which is a textbox.
hoverCp.Text = hoveringUnit.Completions.ToString();
hoverTd.Text = hoveringUnit.Touchdowns.ToString();
hoverIt.Text = hoveringUnit.Interceptions.ToString();
hoverCs.Text = hoveringUnit.Casualties.ToString();
hoverVp.Text = hoveringUnit.MostValuablePlayer.ToString();
hoverSp.Text = hoveringUnit.Spp.ToString();
hoverName.Text = hoveringUnit.Name;

hoverMove.Text = hoveringUnit.Ma.ToString();
hoverStrength.Text = hoveringUnit.St.ToString();
hoverAgility.Text = hoveringUnit.Ag.ToString();
hoverArmour.Text = hoveringUnit.Av.ToString();

hoverSkills.Text = null;
foreach (string skill in hoveringUnit.SkillNames)
{
hoverSkills.AppendText(string.Format("{1}{0}", Environment.NewLine, skill));
}

I'm not sure why simply updating text should take so, comparatively, long. If needed the full game can be found here including graphics, 3.9MB. Many thanks, John

Share on other sites
Updating a label isn't a slow operation, but updating many of them frequently can be, because there's more machinery involved than just setting some text (validation, events, book keeping, and so on). There may be other problems beyond that snippet of code as well.

The thing is, using labels for this purpose isn't ideal. They consume signifigantly more resources (being controls) than a simple text string would, and simply drawing the strings directly on to your render surface is probably more ideal.

Share on other sites
I was thinking about drawing them but using labels seemed more simple to update and layout. I'll update it to draw the text instead.

Cheers!
John

Share on other sites
One problem may be the refresh events of the form and labels.

When you are updating a lot of controls (GDI), tell the form and controls to not redraw until you are done. I think (although I could be wrong) that you can do this with the Layout stuff...

ofrmMyForm.SuspendLayout()
lblValue.SuspendLayout()
lblValue.Text = "Foo..."
lblValue.ResumeLayout()
ofrmMyForm.ResumeLayout()

Try it and see. There may be another property similar to it.