Hello, I got a loop which reads data from mysql server. My server is fine the speed and all but when it comes to looping around 5000 strings it's too slow.
How can I make this loop quicker :
http://i.imgur.com/A4ycVdL.png
Please help me out :)
Printable View
Hello, I got a loop which reads data from mysql server. My server is fine the speed and all but when it comes to looping around 5000 strings it's too slow.
How can I make this loop quicker :
http://i.imgur.com/A4ycVdL.png
Please help me out :)
You didn't need to come here to ask that question. You'd already have your answer if you had read the documentation for the Add method you're calling. ALWAYS read the relevant documentation first.
don't add to the listview in the loop... add to a list(Of t) instead... then after the loop suspendlayout on the lsitview, addrange the list, then resumelayout.
-tg
Hello,
First of all thankyou very much for helping. But I'm really not sure... Believe it or not this is the first time I'm using "For Each", Loops. I just recently discovered it. I'd appreciate it very much if you can provide me an example :). I'll be googling what you meant, hopefully I can figure it out.
You shouldn't really need an example because each step is pretty simple. A bit of an explanation might not go amiss though.
Your original loop is slow because it updates the UI every time you add an item to the list view. Updating the UI is slow so your loop was slow. We're looking for a way of updating your list view... but only updating the UI once.
So the first thing to understand is that a listview is displaying a List of ListItems. The fastest way to add a lot of elements to that List is to use the AddRange function. That will require you to pre-prepare a list of items to add as a range.
Then you SuspendLayout on the ListView. This temporarily prevents the UI from being updated.
Then you shove the items into your ListView using the AddRange function.
And finally turn the UI back on so that your changes take effect in one hit.
First create a List of type ListViewItem. Create individual ListViewItems (exactly as you do when using Add) and add them to the aforementioned List instead of to the ListView.Items collection. When you've added all the items you want you call the AddRange of the ListView as shown here:-
https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
and pass your List into it as a parameter.
the same way you're doing it now, only instead of adding it to the LV directly, you add it to a list...
I suppose you could keep the original code and simply suspendlayout before the loop and then resumelayout after the loop is done... but I typically will use the code with the addrange ...Code:Dim lvItems as new List(Of ListViewItems) '<-- this is the list we'll add to
'dang, you posted a picture of the code so I can't copy it...
'start your for each -- you have that
'create a new listviewtiem -- you have that
lvItems.Add(lsv) ' see, here we add to the list, not the LV directl
' next
lsitview1.suspendlayout ' -- this causes the control to not render everytime we add to it... it suspends the layout of hte control
listivew1.items.addrange(lvItems) ' -- I think that works as-is ... at worst, you'll need lvItems.ToArray
listveiw1.resumelayout ' we're done adding so now we release the control to update itself and it only needs to do it the once.
-tg
I mean read the relevant MSDN documentation before doing ANYTHING else. I'm not sure what's difficult to understand about that. I'm not sure why my pointing out what you did wrong in failing to help yourself would be considered a grudge.
Why would I need to provide a link when you have the VS Help menu right in front of you? It's not like it's hidden.
And did you bother to use the VS Help menu?
Had you done as you should then you'd have seen this:Sounds fairly plain to me. If you'd then followed the links you'd have seen this:Quote:
To add a set of items to the control in a single operation, use the AddRange method. If you want to use the Add method to add a large number of items to the control, use the BeginUpdate and EndUpdate methods to prevent the ListView from repainting until all items are added.
Again, fairly plain. I'm not against people posting on forums or answering their questions but not if they haven't done the obvious things to help themselves first. It would have taken a few minutes at the most to find that information so there's really no excuse for not having done. I was a beginner too and that's exactly how I used to answer most of my own questions. When I first started posting here, I was also able to answer questions for others on unfamiliar topics by doing the same thing. If I can do it, everyone else can too. You can choose to heed this advice and help yourself to become better or you can feel hard done by and wonder why I'm being so mean to you. It depends what's more important to you I guess. Now. Bye.Quote:
Code:' Members are added one at a time, so call BeginUpdate to ensure
' the list is painted only once, rather than as each list item is added.
ListView1.BeginUpdate()
For count = 0 To foodList.Length - 1
Dim listItem As New ListViewItem(foodList(count))
listItem.SubItems.Add(foodPrice(count))
ListView1.Items.Add(listItem)
Next
'Call EndUpdate when you finish adding items to the ListView.
ListView1.EndUpdate()