I have problem with long time drawing controls in my smart device project.

Here is code that freeze application for ~11s:

SAdr = this.ExecuteSQL("select * from dba." + table);
while (SAdr.Read())
{
    PictureBox pBox = new PictureBox();
    pBox.Image = Program.ByteArrayToImage((byte[])SAdr["IMGDATA"]);
    pBox.Name = String.Format("pictureBox{0}#{1}",nameKey ,Int16.Parse(SAdr[colName].ToString()));
    pBox.Width = pBox.Height = size;
    pBox.Left = marginL;
    pBox.Top = marginT;
    pBox.SizeMode = PictureBoxSizeMode.StretchImage;
    pBox.Click += new EventHandler(pBoxTow_Click);

    if (counter < cols)
        marginL += size + space;
    else
    {
        marginL = 10;
        marginT += size + space;
        counter = 0;
    }
    panel.Controls.Add(pBox);
    counter++;
}
panelCenter.Controls.Clear();
panelCenter.Controls.Add(panel);

This time is measure from line SAdr = this.ExecuteSQL("select * from dba." + table); to line with panelCenter.Controls.Clear();.

Any ideas how to improve this code?

有帮助吗?

解决方案

Read the data from the table into an array (bye array probably, to store the images). Then once the data reading is finished, only then begin to create the controls. Populating the picturebox from the local array will be faster than reading each item from the table and simultaenously populating it.

Keep the database operation separate from the GUI operation by making use of arrays.

其他提示

Your single threaded application is getting data from the db, and during that operation your GUI freezes. You should consider accessing the database from a different thread.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top