Results 1 to 10 of 10

Thread: add a lot of items to listview *quickly*

  1. #1

    Thread Starter
    Addicted Member adamm83's Avatar
    Join Date
    Oct 2005
    Location
    Toronto, ON, Canada
    Posts
    180

    add a lot of items to listview *quickly*

    I am running a loop through about 500,000+ items and add the items to an array. I would like to add the items to a ListView at the same time, but the problem is it takes WAY too long. It also takes exponentially longer because I have a label that displays the percentage that is done and because of that I need to put a "DoEvents" statement. The DoEvents statement slows it down a LOTT.

    Is there any way around this.. to make the entire process much quicker?
    adamm
    ACM Designs

    Codebank:
    RegOpen - Open registry keys in Regedit quick & easily


    "A man who tries to catch two rabbits at the same time will catch neither."

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: add a lot of items to listview *quickly*

    It's been a while since I have done classic VB, but if I remember right, you can use the LockWindowUpdate API to disable drawing of the listview, which may speed up the process. Make sure you don't have any sorting on the list view when you are loading the items, as each time you add an item it may resort the list. Finally you should throttle the doevents to only fire after every 5 or 10 thousand items are added, that would probably increase your performance.

    That being said, 500,000 items is a lot for a single list and many/most people will get very frustrated if that have to manually scroll through that man items.

  3. #3

    Thread Starter
    Addicted Member adamm83's Avatar
    Join Date
    Oct 2005
    Location
    Toronto, ON, Canada
    Posts
    180

    Re: add a lot of items to listview *quickly*

    this program not something that any user will be using other than myself. A bit of a personal project/challenge. And aside from that, I won't need to to browse the list very much. It will first list all 500,000 items, then slowly process each item (do whatever I program) and after it finishes each item, it will place a checkmark beside it.

    Thanks for the suggestions. I am going to try them out right now.
    adamm
    ACM Designs

    Codebank:
    RegOpen - Open registry keys in Regedit quick & easily


    "A man who tries to catch two rabbits at the same time will catch neither."

  4. #4
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: add a lot of items to listview *quickly*

    For a list that large, i recommend using a database connection. The listview is capable of connecting to a data-source control.
    You should also consider reading the entire file directly into an array. You are probably losing a lot of time with a lot of disk access. Change your code so it only hits the doevents about once every 1000 items.
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  5. #5
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: add a lot of items to listview *quickly*

    500k is way too many items for a listview. That said, Negative offered some good ideas for speedng things up.

    Instead of LockWindow, you can simply hide the listview by setting its .Visible property to False. Do that before the loop starts, then set it back to True when the loop ends and the display won't even flicker. Unless, of course, you use DoEvents to refresh the display. I wouldn't recommend DoEvents anyway; a simple .Refresh method on the control you want refreshed is usually sufficient. (Refresh fails if you maximize some other app over yours and then minimize it again; your app will sit there with a blank screen if you use Refresh, whereas DoEvents would repaint it.)

    Instead of worrying about LockWindow and the DoEvents/Refresh debate, you'd be much better off just using a progressbar control and not worrying about it. Progress bars handle their own refreshing internally.

  6. #6
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: add a lot of items to listview *quickly*

    This is also a fine example of vb2005 express making your job easier. The .net listview can be loaded directly from an array, and has the lock built-in (beginupdate, endupdate). It's a free download...
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  7. #7

    Thread Starter
    Addicted Member adamm83's Avatar
    Join Date
    Oct 2005
    Location
    Toronto, ON, Canada
    Posts
    180

    Re: add a lot of items to listview *quickly*

    ok wow just adding this line of code increased the speed a crazy amount!!
    Code:
    If i Mod 10000 = 0 Then DoEvents
    THANKS! Unfortunately, it was only quick when I wasn't adding the items to the listview.

    After adding the code to add the items to the listview, then adding the LockWindowUpdate api and applying it for the listview, there WAS a noticeable increase in speed, although I think vb has a problem with that many items loaded in a listview. I tested the program through VB, it seemed to load the items fine (took about a minute or 2 ) but after it loaded it, VB and the program was running super slow. I opened the task manager and VB6.exe was using about 80MB and at one point it shot up to 160MB When I tried to close the program, vb seemed to have trouble releasing the memory and it could not properly close my program and return back to the vb design view. I had to kill the vb6.exe task through the task manager.

    instead, I think I might load the items in sections and use the listview iwth a combobox. So I would have the combobox hold something like this:
    1 to 10,000
    10000 to 20000
    20000 to 30000
    etc, etc

    then when it selects one of them, it would load that range into the listview. This will speed things up a lot.

    DOes anyone have a better solution for my needs?
    Last edited by adamm83; Jun 21st, 2007 at 10:17 PM.
    adamm
    ACM Designs

    Codebank:
    RegOpen - Open registry keys in Regedit quick & easily


    "A man who tries to catch two rabbits at the same time will catch neither."

  8. #8
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,621

    Re: add a lot of items to listview *quickly*

    i was actually getting an overflow error with 600,000 so you must be pushing the limits.
    Here's a cheap way of doing this:
    your array is 1 to about 300,000 right?
    well take that and fill it up first,
    then display only 20 items from it at a time in the list box.
    You can place a scrollbar control next to it and set to the correct range(1 to 299980) with a step of 20 and use it to know what part of the array to load the list box with. Every scroll of the arrows would delete the listview and reload it from the new location. You could expand on this to make the user interface work the way you want after you get this part working. (e.g. pageup and down from inside the listview)
    My light show youtube page (it's made the news) www.youtube.com/@artnet2twinkly
    Contact me on the socials www.facebook.com/lordorwell

  9. #9

    Thread Starter
    Addicted Member adamm83's Avatar
    Join Date
    Oct 2005
    Location
    Toronto, ON, Canada
    Posts
    180

    Re: add a lot of items to listview *quickly*

    I am using a multidimensional array. It's 700 by 765, so the max is 535500, but it can be lower. I haven't run into any overflows yet, but i know it's close.

    the scrollbar idea is a good one. I'll try that
    adamm
    ACM Designs

    Codebank:
    RegOpen - Open registry keys in Regedit quick & easily


    "A man who tries to catch two rabbits at the same time will catch neither."

  10. #10
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,710

    Re: add a lot of items to listview *quickly*

    If you hide the listview during the loading it and make it visible again after its done you will see much more improvements. Try using paging for it so its like you stated in groupos instead of all in one.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width