Forum > LCL

Latency when showing secondary window

(1/1)

rpetges:
I have an application where an initially created, but not visible secondary form is used to track changes that occur during processing. During processing, thousands of items get added into a listview. All this works fine.

When I try to make the window visible, it takes lot of time ( up to 30 seconds, depending on the number of items ) before the window is finally shown. When I create the window in visible state before adding items and then close it, I do not notice this latency, it shows immediately.

I attached a quick and dirty small application to show the issue:

- Working as expected: Start the small test application, click on "ShowForm2", close "Form2" and then click on "Add 30000 items". Now click on "ShowForm2" again and the form opens immediately.

- Working NOT as expected: Start the small test application, click on "Add 30000 items" and then on the "ShowForm2" button. It will take up to 5 seconds to display the form.

Is there a way to show the hidden form immediately after having added the items in hidden state ?

Best regards,
Romain

wp:
TListView becomes very slow when it contains many items. I see this even in the case that you entitle "Working as expected" because it takes very long to populate the listview.

Switching the ListView to virtual mode, however, you will get an astonishing speed: Don't store the data in the ListView, but somewhere else, e.g. in a TStringList. Set ListView.OwnerData to true. Write a handler to ListView.OnData which is called whenever a listitem is needed: fill the item's property from data in your external storage. And finally tell the Listview the number of items: ListView.Items.Count := (count_in_external_storage).

Applied to your sample:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TForm3 = class(TForm)    ListView1: TListView;  // External storage for the listview items    procedure FormCreate(Sender: TObject);    procedure FormDestroy(Sender: TObject);    procedure ListView1Data(Sender: TObject; Item: TListItem);  private  public    List: TStringList;  end;... procedure TForm3.ListView1Data(Sender: TObject; Item: TListItem);begin  Item.Caption := List[Item.Index];end; procedure TForm3.FormCreate(Sender: TObject);begin  List := TStringList.Create;   // The following properties could be set in Object Inspector, too...  ListView1.OwnerData := true;  ListView1.OnData := @ListView1Data;end; procedure TForm3.FormDestroy(Sender: TObject);begin  List.Free;end;
Code to populate the list:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.Button4Click(Sender: TObject);var  MyCount: integer;begin  // Store the items to the stringlist rather than to the listview  Form3.List.Clear;   for MyCount := 1 to 30000 do  begin    Form3.List.Add('Caption ' + IntToStr(Mycount));  end;   // Tell the listview the number of items   Form3.ListView1.Items.Count := Form3.List.Count;   ShowMessage('All 30000 items added');end;
You will notice zero latency...

rpetges:
Many thanks for your suggestion ... I also noticed a real slowdown when adding thousands of items.

I'll try your idea but need to find a way to store multiple columns in an external data object. In fact, in my real application I have 12 columns in the listview. Do you have a suggestion ?

Many thanks for your help.

wp:
Combine all texts of each row into a single string. Use a well-defined character (tab?) as separator. Store these strings in the stringlist as in the previous example.

In the OnData event read the requested string from the stringlist and split it at the separator positions into several parts. The first part must be assigned to the Caption, and the others must be added as SubItems of the ListItem provided by the OnData event.

See attached modified sample code with a virtual listview having 5 columns (1 caption + 4 subitems).

rpetges:
Good idea, will try to implement the virtual listview in my application ... this should speed up things a lot :-)

Many thanks wp!

Navigation

[0] Message Index

Go to full version