views:

329

answers:

5

Hello,

I have a ListView:TListview on my form ,and, I add many values(approximately 25k TListViewItem) ,which works quite fast,but when I call Listview.Clear,the program freezes.I checked it with debugger,it won't step that line.

My question is,how do I solve my problem? If creating so many items in less than a second is possible,why deleting them takes forever(i waited over 5 minutes)?

Thanks in advance.

+5  A: 

The first thing I'd try is wrap your call to Clear with BeginUpdate/EndUpdate.

  ListView1.Items.BeginUpdate;
  ListView1.Clear;
  ListView1.Items.EndUpdate;

Do you have any events attached to the ListView, and are they firing as the list is being cleared?

Bruce McGee
No,i used begin/end update at first place.Its the same.
John
That time is being spent somewhere. Are any events firing? I'd use breakpoints or CodeSite/OutputDebugString messages to see. I like CodeSite for this because it has an IDE plug-in to put EnterMethod/ExitMethod messages in all (or a selection of) your methods in a unit.
Bruce McGee
+13  A: 

Have you tried enclosing your call to Clear in a BeginUpdate/EndUpdate block:

  listview.Items.BeginUpdate;
  try
    listview.Items.Clear;
  finally
    listview.Items.EndUpdate;
  end;

Adding/Removing items in a listview (or various other controls, e.g. listbox) triggers a GUI update of the control for each and every item that is added/removed. For a listview in particular, this can be quite expensive and for 25,000 items the overhead would be significant.

Admittedly 5 minutes does sound excessive, but this would be the first thing I would try.

Deltics
+2  A: 

Hello, as the others noted a BeginUpdate .... EndUpdate will greatly increase performance, however I would really suggest you move your code to use VirtualTreeView. It's a hybrit tree/ListView which will add up to 1m nodes in less than a second (actually that depends on the processor, but you get the idea).

It's a bit harder to learn in the beginning but once you get used to it you'll find it "easy" to work with. I personally whenever I need many rows in a ListView or TreeView look no further than VirtualTreeView. Oh, and forgot to mention that on top of it, it's free. Try it from : http://soft-gems.net/

Aldo
+2  A: 

John, it should not be longer to clear than to add the 25k items.
I wonder if you load it while it is not visible (automatically disabling updates), but clear it when it is visible where each item deletion triggers an update.

François
It's visible.BeginUpdate/EndUpdate is user.
John
A: 

I don't know whether I should delete this question or not,I believe it's not going to be in any use to anyone else,but I prefer to keep it for awhile at least for you,who answered.

The problem was that I used a component inheriting from TListView,I thought It wouldn't be a problem so I decided to say TListView,but I was wrong.

I upvoted all your answers, please excuse my ignorance - I'm new.

John
Don't sweat it. So, what is the component you're using? Something home grown, or from a third party?
Bruce McGee
AlphaControls,it's awhole skin package with controls inheriting from borland's vcl and their skinView(TsSkinView) is quite bad.
John
What was the problem exactly? Was their clear code so slow? Why was it?
Smasher
No,it just freezes if you call Listbox.clean and there are many items on the listbox.
John
Maybe dereferencing a nil pointer? That causes most of the freezes for me.
Smasher