Author Topic: Delphi + listbox  (Read 14159 times)

Spike

  • Guest
Delphi + listbox
« on: 3 Sep '03 - 09:43 »
hi there,just a small question:
im using a listbox for my playlist,but when i try to load a whole directory with +300 mp3's into it,it takes ages for the listbox to load,and while loading the data the rest of the program freezes,but will continue after every file is loaded into the listbox,

is there a way to load items faster? maybe a custom component or something? im using Delphi 6 btw
Tnx in advance

Onix

  • Posts: 3
Re: Delphi + listbox
« Reply #1 on: 3 Sep '03 - 11:17 »
Hi Spike,
 I had the same problem. I used Application.ProcessMessage into the loop that write items into listbox. Also you can call ListBox.StartUpdate(?) and EndUpdate(?) to draw in one time all the items.
So your program don't freeze during adding items.

Bye

Giancarlo

ps: sorry for my bad english :(

AndrewJameson

  • Guest
Re: Delphi + listbox
« Reply #2 on: 3 Sep '03 - 15:16 »
Why do people keep insisting on using Application.ProcessMessages ? - it can really cause a lot of problems ... for example you can press and button and this will cause code re-entrancy problems and I've seen it more frequently in loops :

repeat
Application.ProcessMessages;
until <condition>

Great ... uses 100% cpu resources !!!

To correct the previous response, you need to bracket the ListBox items addition with ListBox.Items.BeginUpdate and ListBox.Items.EndUpdate - and forget processing messages ... if you need to get something to Update then use the Update method ... ListBox.Update !


Andrew

Dimitris Touzozoglou

  • Guest
Re: Delphi + listbox
« Reply #3 on: 4 Sep '03 - 12:11 »
Maybe you can post the code you are using.
Application.ProcessMessages is a MUST if you want the user to be able to cancel, good programing guidelines is that you use try..finally and you MUST disable and after that enable the controls you do not want the user to play with, also you have to change the cursor to crHouglass and then back to cdDefault.
All these are very very basic steps every software must have in order to be usable otherwise you end up with crapy implementations..

Spike

  • Guest
Re: Delphi + listbox
« Reply #4 on: 4 Sep '03 - 12:52 »

Quote

Maybe you can post the code you are using.
Application.ProcessMessages is a MUST if you want the user to be able to cancel, good programing guidelines is that you use try..finally and you MUST disable and after that enable the controls you do not want the user to play with, also you have to change the cursor to crHouglass and then back to cdDefault.
All these are very very basic steps every software must have in order to be usable otherwise you end up with crapy implementations..



actually,the 2nd post solved it,and imo its up  to the designer/coder if he wants to change the cursor to an hourglass or not,atm it loads the list so fast that changing it to an hourglass is just some waste of code,but anyways,different coders ...diffirent opinions :)

Dimitris Touzozoglou

  • Guest
Re: Delphi + listbox
« Reply #5 on: 5 Sep '03 - 08:32 »
BTW
BeginUpdate..EndUpdate is used in Stringlists in Delphi to 'tell' controls not to repaint, so adding items is a lot faster.
But you should always consider a user with a slow machine and a huge count of files in a folder or a similar case.
And you have to do some disabling of controls to prevent accidental clicks on the same button ...
Enough said I think  ;D

AndrewJameson

  • Guest
Re: Delphi + listbox
« Reply #6 on: 5 Sep '03 - 12:24 »
If you want to be able to cancel operations that might take a while then you MUST not rely on Application.ProcessMessages ... it should NEVER be used when other methods exist and it definitely NOT good practice ... it's use is a can of worms ... reentrancy problems are significant and to overcome multiple button pressing you end up with a mass of code just to control events ...

So that's why threads exist ... if you've got something that essentially takes a while then put it in a thread.

(In this case, I see no value in making the process of filling a listbox with filenames into a major task ... even a few hundred can be loaded in the twinkling of the eye even on 'slow' machines.)

Andrew