Ivan Prodanov
Ivan Prodanov

Reputation: 35532

TListView.Clear freezes my application,how do i fix it?

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)?

Upvotes: 2

Views: 2043

Answers (5)

Ivan Prodanov
Ivan Prodanov

Reputation: 35532

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.

Upvotes: 0

Francesca
Francesca

Reputation: 21650

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.

Upvotes: 2

zz1433
zz1433

Reputation: 3586

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/

Upvotes: 2

Deltics
Deltics

Reputation: 23056

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.

Upvotes: 14

Bruce McGee
Bruce McGee

Reputation: 15334

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?

Upvotes: 5

Related Questions