luca.p.alexandru
luca.p.alexandru

Reputation: 1750

I need to draw 10000 Points in a Panel without blocking the UI

I need to draw 10000 Points in a Panel without blocking the UI. I am using C#. Currently I am doing the drawing in the panel Paint event. How can I do this without blocking the UI. I already tried doing the "painting" on a separate Thread but failed to succeed

private void Panel1Paint(object sender, PaintEventArgs paintEventArgs)
    {
        var g = paintEventArgs.Graphics;
        g.DrawLine(new Pen(Color.Black),
                   new Point(0, panel1.Width / 2),
                   new Point(panel1.Height, panel1.Width / 2));
        g.DrawLine(new Pen(Color.Black),
                   new Point(panel1.Width / 2, 0),
                   new Point(panel1.Width / 2, panel1.Height));

        for (int i = 0; i < centres.Length; i++)
        {
            g.FillEllipse(new SolidBrush(colors[i]), centres[i].X, centres[i].Y, 10, 10);
            Console.Out.WriteLine(centres[i].ToCart());
        }

        for (int i = 0; i < 10000; i++)
        {
            int zona = r.Next(0, 3);
            double p_gauss, p_rand;
            int new_x;
            int new_y;
            do
            {
                new_x = r.Next(0, 400);
                p_gauss = Gauss(new_x, centres[zona].X, s[zona].X);
                p_rand = r.NextDouble();
            } while (p_gauss < p_rand);

            do
            {
                new_y = r.Next(0, 400);
                p_gauss = Gauss(new_y, centres[zona].Y, s[zona].Y);
                p_rand = r.NextDouble();
            } while (p_gauss < p_rand);

            g.DrawEllipse(new Pen(colors[zona], 2), new_x, new_y, 1, 1);

        }

    }

Upvotes: 1

Views: 76

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273574

Do your painting on a Bitmap, in a Thread. Make the finished bitmap available to your Form and let the Paint event draw the whole bitmap at once.

Upvotes: 1

Related Questions