views:

260

answers:

5

I am using file.readalllines to read the file into a string, then using listbox.items.addrange and splitting by vbcrlf to insert the items. Is there a way to reduce the lag it causes for loading huge lists?

+1  A: 

Use the BeginUpdate and EndUpdate methods; this will disable redrawing of the listbox temporarily and can speed up the process of populating it.

ListBox1.BeginUpdate()
' fill the list using Items.Add or Items.AddRange '
ListBox1.EndUpdate()
Fredrik Mörk
This will help with speed, but not responsiveness of course. If the file is sufficiently large, there's no real point to do this.
Noldorin
+4  A: 

Given that the file is large enough to cause unresposiveness by reading it atomically, it would seem that you should be doing the reading in a separate thread.

A thread-pool item should do the job here. Try this:

ThreadPool.QueueUserWorkItem((state) =>
     using(var streamReader = new StreamReader("foo.txt"))
     {
         string line;
         while ((line = streamReader.ReadLine()) != null)
             listBox.Invoke(() => listBox.Items.Add(line));
     })

(My VB.NET is a bit rusty, so if someone could convert that, that would be helpful.)

However, the fact that you have to call Invoke to cross the thread-boundary to update the GUI will mean that your performance will actually decrease, though at least you'll get normal responsiveness with the UI!

The other obvious option is to use BackgroundWorker, which is designed to perform long tasks in the background in UI applications. However, I'm not sure it offers any advantages in this case, and a simple thread pool item would be significantly simpler and allows you to do everything inline.

Noldorin
+1  A: 

You can try to fill the listbox using another thread. This will avoid the "hang", leting the user interact with the another form controls. You can also load itens partially instead of load all data at once.

Eduardo Cobuci
+1  A: 

Use a BackgroundWorker and load the file in a seperate thread. Be careful about adding the lines to the ListBox from the thread. You'll need to post the items from the correct thread.

Bob King
+1  A: 

file.readalllines to read the file into a string

No! File.ReadAllLines reads into a string array.

splitting by vbcrlf

So this is already done for you: you're doing a bunch of extra work for nothing.

Joel Coehoorn
Yeah, that was the part where I was wondering what he really is doing :)
VVS