Excessive use of pens C # [closed]

3

Good morning, friends,

I'm developing an application that should draw 4 graphics (for now), to draw the way you want it to need to redraw the graphics several times and update them. With too much drawline, performance drops. When I stop drawing, all my app runs normal, but when it starts to draw everything goes slow until it stops completely (including the buttons). Please, does anyone know a way to optimize this code? Any tips? Thank you very much!! Below are the main parts of the code.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    //metodo que chama o controle que desenha o gráfico---------------------------------------
    private void SerialCOM_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        if (SerialCOM.IsOpen)
        {
            SerialPort sData = (SerialPort)sender;
            {
                string RxString = sData.ReadLine();
                bool result = int.TryParse(RxString, out int data);
                if (result)
                {
                    var atualdata = 5.0f * data / 1023.0f;

                    //CHAMADA DO CONTROLE
                    graphControl1.AddValue(atualdata, atualdata, atualdata, atualdata);

                    if (controlegravar == 1 && aquisicaodata.Count <= 6000) aquisicaodata.Add(atualdata);
                }
            }
        }
    }
}
//CONTROLE------------------------------------------------------------------------

public class GraphControl : Control
{

    public void AddValue(float value1,float value2,float value3,float value4)
    {
        _values1.Add(value1);
        _values2.Add(value2);
        _values3.Add(value3);
        _values4.Add(value4);
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        var wid = (float)Width / (float)_dataCount;
        var total = 5;

        //-----------------------------------------1--------------------------------------------------------------------

        var lastPoint1 = new PointF(0F, (float)(2*Height/5) * 0.5F);
        var lastPoint2 = new PointF(0F, (float)(Height / 2));
        var lastPoint3 = new PointF(0F, (float)(7 * Height / 10));
        var lastPoint4 = new PointF(0F, (float)(9 * Height / 10));
        PointF newPoint1 = new PointF();
        PointF newPoint2 = new PointF();
        PointF newPoint3 = new PointF();
        PointF newPoint4 = new PointF();

        var idInit = _values1.Count - _dataCount;

        if (idInit < 0)
            idInit = 0;

        for (int i = idInit, a = 0; i < _values1.Count; i++, a++)
        {
            var value = (float)(_values1[i]);
            var porcent = value / total;

            if (porcent > 1) porcent = 1 ;
            var hg = porcent * (2*Height/5);

            newPoint1.X = (a + 1) * wid;

            if (porcent >= 1) newPoint1.Y = 2*Height/5 - hg;
            else newPoint1.Y = 2*Height/5 - hg - 1;

            using (var p = new Pen(Color.Lime, 0.1f)) e.Graphics.DrawLine(p, lastPoint1, newPoint1);

            lastPoint1 = newPoint1;

            //------------------------------------------------------------------------------------

            value = (float)(_values2[i]);
            porcent = value / total;

            hg = porcent * (Height / 5);

            newPoint2.X = (a + 1) * wid;

            newPoint2.Y = 3 * Height / 5 - hg;

            using (var p = new Pen(Color.Lime, 0.1f)) e.Graphics.DrawLine(p, lastPoint2, newPoint2);

            lastPoint2 = newPoint2;

            //------------------------------------------------------------------------------------

            value = (float)(_values3[i]);
            porcent = value / total;

            hg = porcent * (Height / 5);

            newPoint3.X = (a + 1) * wid;

            newPoint3.Y = 4 * Height / 5 - hg;

            using (var p = new Pen(Color.Lime, 0.1f)) e.Graphics.DrawLine(p, lastPoint3, newPoint3);

            lastPoint3 = newPoint3;

            //------------------------------------------------------------------------------------

            value = (float)(_values4[i]);
            porcent = value / total;

            hg = porcent * (Height / 5);

            newPoint4.X = (a + 1) * wid;

            newPoint4.Y = Height - hg;

            using (var p = new Pen(Color.Lime, 0.1f)) e.Graphics.DrawLine(p, lastPoint4, newPoint4);

            lastPoint4 = newPoint4;
        }

        base.OnPaint(e);
    }
}

Detail: The datareceived method is called by a timer with an interval of 3 ms. It needs to be very fast.

    
asked by anonymous 18.12.2018 / 15:15

1 answer

1

A refresh rate of 3ms is greater than the refresh rate of the screen at either 60fps (16.6ms) or 120fps (8ms). Even if you force Invalidate every 3 ms, it will not refresh at the same time. Try to optimize the datareceived to function asyncronous and see the possibility of or increase the update time or leave the Invalidate call async.

    
19.12.2018 / 18:10