Reducing time in C # Forms Control.set_Text function (string)

Hope for a quick answer (which seems to be very good for ...)

I just did a performance analysis with VS2010 in my application, and it turns out that I spend about 20% of my time on a function Control.set_Text(string), as I update shortcuts in several places in my application.

There is a timer object in the window (a form timer, not a Threading timer) that has a callback timer1_Tickthat updates each label every tick (to give a stop-war effect) and updates about 15 shortcuts once a second.

Does anyone have quick suggestions for reducing the time taken to update text on a form other than increasing the refresh interval? Are there other structures or functions that I should use?

+3
source share
2 answers

I came across this question and created my own simple Label control.

The .NET Label control is a surprisingly complex beast, so it is slower than we would like.

You can create a class that inherits Control, call SetStylein the constructor to make it double-buffered and colored by the user, then override the method OnPaintto call e.Graphics.DrawStringand draw the Textproperty.
Finally, override Textor TextChangedand call Invalidate.

Until you need AutoSize, it will be significantly faster than the standard Label control.

Here is my implementation: (Currently used in production)

///<summary>A simple but extremely fast control.</summary>
///<remarks>Believe it or not, a regular label isn't fast enough, even double-buffered.</remarks>
class FastLabel : Control {
    public FastLabel() {
        SetStyle(ControlStyles.AllPaintingInWmPaint
               | ControlStyles.CacheText
               | ControlStyles.OptimizedDoubleBuffer
               | ControlStyles.ResizeRedraw
               | ControlStyles.UserPaint, true);
    }
    protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); Invalidate(); }
    protected override void OnFontChanged(EventArgs e) { base.OnFontChanged(e); Invalidate(); }

    static readonly StringFormat format = new StringFormat {
        Alignment = StringAlignment.Center,
        LineAlignment = StringAlignment.Center
    };
    protected override void OnPaint(PaintEventArgs e) {
        e.Graphics.DrawString(Text, Font, SystemBrushes.ControlText, ClientRectangle, format);
    }
}

, < <29 > .

+6

, Text 0,6 , . , . , AutoSize . , , , , - TrueType, , , ABC ClearType. Windows, . , , . , . , Aero , , , , .

, .

, Windows Forms, . OnPaint, TextRender.DrawText, . , 50 . .

+2

Source: https://habr.com/ru/post/1742715/


All Articles