0

I am Using following function to Add items.this function working proparly but it takes much time to execution.please help me to reduce the execution time of items add in listview.

Function:

listViewCollection.Clear();
listViewCollection.LargeImageList = imgList;
listViewCollection.LargeImageList.ImageSize = new System.Drawing.Size(100, 100);
 foreach (var dr in Ringscode.Where(S => !S.IsSold))
 {
     listViewCollection.Items.Insert(0,
                   new ListViewItem(dr.CodeNo.ToString(), dr.RingID.ToString()));
     imgList.Images.Add(dr.RingID.ToString(), binaryToImage(dr.Image));
 }

public Image binaryToImage(System.Data.Linq.Binary binary) 
{ 
   byte[] b = binary.ToArray(); 
   MemoryStream ms = new MemoryStream(b); 
   Image img = Image.FromStream(ms); 
   return img; 
}
yogi
  • 19,175
  • 13
  • 62
  • 92
Tulsi
  • 151
  • 3
  • 15
  • I guess time consuming method is `binaryToImage` which you haven't shared. – yogi Aug 04 '12 at 11:27
  • public Image binaryToImage(System.Data.Linq.Binary binary) { byte[] b = binary.ToArray(); MemoryStream ms = new MemoryStream(b); Image img = Image.FromStream(ms); return img; } – Tulsi Aug 04 '12 at 11:28
  • @yogi binaryToImage return image – Tulsi Aug 04 '12 at 11:29
  • Did you try to change your approach, I mean you could load those images to `imgList` on demand when they are needed and at that time perform `binaryToImage` conversion instead of doing that in a loop. – yogi Aug 04 '12 at 11:42
  • One good approach could be to save paths of files in database instead of there binary forms, in that way there wouldn't be need of any conversion in code :) – yogi Aug 04 '12 at 11:45
  • Have a look at this post http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay/3751#3751 – yogi Aug 04 '12 at 11:56
  • Get your rows in the correct order and use add instead of insert might have some impact. – Tony Hopkinson Aug 04 '12 at 11:58
  • imgList.Images.Add(dr.RingID.ToString(), binaryToImage(dr.Image)); Can i usd AddRange insted of Add? – Tulsi Aug 04 '12 at 12:02

1 Answers1

1

You need to expect that your UI is going to be slow if you do nonUI work in same thread (in your case, manipulating streams and images). Solution would be to offload this work to another thread, leaving UI thread to user. Once worker thread finishes, tell UI thread to do the update.

Second point is that when updating ListView data in batch, you should tell ListView to wait until you finish all manipulation.

Better if you do it like this. Commentary is inline.

// Create a method which will be executed in background thread,
// in order not to block UI
void StartListViewUpdate()
{
    // First prepare the data you need to display
    List<ListViewItem> newItems = new List<ListViewItem>();
    foreach (var dr in Ringscode.Where(S => !S.IsSold))
    {
        newItems.Insert(0,
                      new ListViewItem(dr.CodeNo.ToString(), dr.RingID.ToString()));
        imgList.Images.Add(dr.RingID.ToString(), binaryToImage(dr.Image));
    }

    // Tell ListView to execute UpdateListView method on UI thread
    // and send needed parameters
    listView.BeginInvoke(new UpdateListDelegate(UpdateListView), newItems);
}

// Create delegate definition for methods you need delegates for
public delegate void UpdateListDelegate(List<ListViewItem> newItems);
void UpdateListView(List<ListViewItem> newItems)
{
    // Tell ListView not to update until you are finished with updating it's
    // data source
    listView.BeginUpdate();
    // Replace the data
    listViewCollection.Clear();
    listViewCollection.LargeImageList = imgList;
    listViewCollection.LargeImageList.ImageSize = new System.Drawing.Size(100, 100);
    foreach (ListViewItem item in newItems)
        listViewCollection.Add(item);
    // Tell ListView it can now update
    listView.EndUpdate();
}

// Somewhere in your code, spin off StartListViewUpdate on another thread
...
        ThreadPool.QueueUserWorkItem(new WaitCallback(StartListViewUpdate));
...

You may need to fix a few things as I wrote this inline, and didn't test it in VS.

Nikola Radosavljević
  • 6,871
  • 32
  • 44