1

I am trying to used following code but I am not getting good performance in my Application.

I have list of 2000 images in “dt_Images.ToList()”.

In cs:

foreach (var dr in dt_Images.ToList())
            {
                BTN = new Button();
                BTN.Name = dr.Name.ToString();
                BTN.Image = dr.Image;
                BTN.Text = dr.text.ToString();
                flowLayoutPanel1.Controls.Add(BTN);
                BTN.Click += new EventHandler(this.pic_Click);
            }

I have also try to binding with listview but not getting desired speed.

How can I increase speed while binding with flowLayoutPanel?

can i bind direct DataSource in control? then which control i used?

Please help me

Tulsi
  • 151
  • 3
  • 15
  • Images are stored as BLOB data in database and are heavy weight. Is it possible to prefetch this information and hold this in your memory. 2000 is not a big chunk provided your images are reasonably small (in KB and not in MB) – questzen Mar 28 '12 at 11:43
  • my All image are in KB but taking 2 minite to load this form. @questzen – Tulsi Mar 28 '12 at 12:00
  • database fetch is time consuming. You can cutdown the turnaround time by caching the images in local memory. Try to initialize these 2000 objects before hand if your application allows. – questzen Mar 28 '12 at 12:04
  • my foreach loop taking 2 minite to execut.can it possible to removed foreach loop? @questzen – Tulsi Mar 28 '12 at 12:12

4 Answers4

1

I think, usually you only need to download the picture you show and the few around. So if you download the 5 afters and the 5 before the current, your speed gonna be ok.

ykatchou
  • 3,667
  • 1
  • 22
  • 27
0

We can not always improve performance, but we can definitely improve perceived performance. Assuming you are developing a UI application, please refer to the following logic.

As part of application start up, try to fetch the all the images and relevant data. This is time consuming, but don't worry. Cache these objects in memory (HashMap?). When you find the need to create a flow panel for a user, try to do a look up in the cache and quickly render the image.

The sql you are executing forces a database round trip. Try to do a 'select * ' without where condition (bulk fetch) for populating the cache.

questzen
  • 3,260
  • 18
  • 21
0

Perhaps you should only create a button when you are about to use it. (Or a few of them as in ykatchou's answer.)

Even if you do want to create them all at once – you might be better off creating them using a BackgroundWorker.

Community
  • 1
  • 1
ispiro
  • 26,556
  • 38
  • 136
  • 291
0

I can see that you are using a FlowLayout panel. I suspect it to redo the whole layout each time you add a child control, which might take a lot of time too.

Can you try to enclose the whole loop this way :

flowLayoutPanel1.SuspendLayout();

foreach (var dr in dt_Images.ToList())
        {
            BTN = new Button();
            BTN.Name = dr.Name.ToString();
            BTN.Image = dr.Image;
            BTN.Text = dr.text.ToString();
            flowLayoutPanel1.Controls.Add(BTN);
            BTN.Click += new EventHandler(this.pic_Click);
        }

flowLayoutPanel1.ResumeLayout();

You can even try to achieve even better performances by disabling the painting using the answers here.

Community
  • 1
  • 1
Larry
  • 17,605
  • 9
  • 77
  • 106