1

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?

WooCaSh
  • 5,180
  • 5
  • 36
  • 54
  • Are you sure that it isn't just `SAdr = this.ExecuteSQL("select * from dba." + table);` line that is causing the delay? If that's the case then the optimization should be done on database retrieval... How long does it take when you execute `select * from dba.tableName` query in SSMS? – Ivan Golović Sep 26 '12 at 10:53
  • Is ti slowing down when invoking `ByteArrayToImage`? I suppose it reads image data which could be kilobytes in size... – Ivan Golović Sep 26 '12 at 10:57
  • @IvanG this delay is generated by code between 1st line. It isn't query. Method ByteArrayToImage slowing too but when comment it i get ~3sec less delay. – WooCaSh Sep 26 '12 at 11:09
  • If could be spending time redrawing the screen. What if you move the line panelCenter.Controls.Clear(); to the start of your routine, or else using panel.SuspendLayout() at the start and panel.ResumeLayout() at the end? – sgmoore Sep 26 '12 at 11:14
  • I will check it, and back with result. Hold on. – WooCaSh Sep 26 '12 at 11:17

2 Answers2

1

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.

george.zakaryan
  • 960
  • 1
  • 6
  • 18
  • 1
    Telling the man how do to it would me a richer post - consider it please. See this link: http://www.tutorialspoint.com/asp.net/asp.net_multi_threading.htm – TheGeekZn Sep 26 '12 at 10:50
1

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.

Mamta D
  • 6,310
  • 3
  • 27
  • 41
  • So You propose to make special class for database operations? I must use threading? I Ask because I'm beginner and don't know nothing now about it. – WooCaSh Sep 26 '12 at 11:15
  • No, no. To keep it simple, you can continue with the same class (though ideally you should have separate class). What I suggest is you create a blank bytes array. Load your images from database into this array. Close database. Then populate your picturebox. – Mamta D Sep 26 '12 at 11:25
  • It's now 6s. Maybe I should change pictures in database. All now have 600px width. It's too large for database? I use in application picture with 100x100 max 150x150. Should I resize it? – WooCaSh Sep 26 '12 at 11:49
  • Yes, see this thread for help regarding this: http://stackoverflow.com/questions/9133399/image-loading-and-stream-file-dependency – Mamta D Sep 26 '12 at 12:41
  • Thanks. I think Your information was helpful, so I check this answer as accepted. I will try to get better timings. Now it's `3-4s` – WooCaSh Sep 26 '12 at 18:44