Post news RSS Some experiments pt2

After having tested how to add events to items on a form, which change properties of their parents, I've now been testing how to generate items on a form and also how to remove them without consuming too many system resources.

Posted by on

Hey all,

After having tested how to add events to items on a form, which change properties of their parents, I've now been testing how to generate items on a form and also how to remove them without consuming too many system resources.

Firstly, we need to look at what consumes alot of system resources. Generating new items on a form costs alot of resources, and could quickly overload your CPU and memory. To prevent this from happening, you need to keep the items stored somewhere. This sounds as a contradiction, but temporarily storing them somewhere else requires less resources than deleting them and regenerating them.

The code which does this is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace StarApocTheBeginning
{
    public partial class Systemview : Form
    {
        public Systemview()
        {
            InitializeComponent();
            generateSystemView();
            InitButtons();
        }

        PictureBox[,] systemview = new PictureBox[8, 8];
        Button Insertbutton = new Button();
        Button Undock = new Button();

        private void generateSystemView()
        {
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    systemview[i, j] = new PictureBox();
                    systemview[i, j].BackColor = Color.Black;
                    systemview[i, j].Location = new Point((32 * i) + 10, (32 * j) + 10);
                    systemview[i, j].Size = new Size(32, 32);
                    systemview[i, j].BorderStyle = BorderStyle.FixedSingle;
                }
            }
        }

        private void InitButtons()
        {
            Insertbutton.Location = new Point(320, 10);
            Insertbutton.Size = new Size(80, 30);
            Insertbutton.Text = "Insert";
            Insertbutton.Click += new EventHandler(InsertSystemView);
            this.Controls.Add(Insertbutton);
            Undock.Location = new Point(320, 45);
            Undock.Size = new Size(80, 30);
            Undock.Text = "Undock";
            Undock.Click += new EventHandler(RemoveSystemView);
            this.Controls.Add(Undock);

        }

        private void InsertSystemView(object sender, EventArgs e)
        {
            this.Controls.Clear();
            this.Controls.Add(Insertbutton);
            this.Controls.Add(Undock);
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    this.Controls.Add(systemview[i,j]);
                }
            }
        }
        private void RemoveSystemView(object sender, EventArgs e)
        {
            this.Controls.Clear();
            this.Controls.Add(Insertbutton);
            this.Controls.Add(Undock);
        }
    }
}

The resulting thing is this:

At startup

If we click the 'Insert' button, the following appears:

insert clicked

If you click the Undock button, the view is restored to the initial state.

This application consumes far less system resources, because it keeps the items stored in the array. This way the garbage collector won't clean it up, forcing you to create new items.

If you have any questions about this, feel free to comment.

Cheers,
Roberto

Post a comment
Sign in or join with:

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.