I'm wondering what would be the best way to populate a ListBox control of a WinForm, populated depending on a radio btn?
I have seen some suggestions to use a foreach to loop over each object of my list, and Add() them to the listBox.items.Add(), but this seems to be a really bad idea, since the list from rabio btn 1 returns a list with 10.000 records (takes quiet some time to loop over, and the UI freeze while looping, bad bad idea).
Is there any better way to do this, and maybe in a seperated Task to stop UI freeze??
private void PopulateListBox()
{
foreach (var item in Controller.ReturnBigResultSet())
this.Invoke((MethodInvoker)(() => listBox1.Items.Add(item)));
}
UPDATE: Code block using AddRange:
var watch = new Stopwatch();
watch.Start();
var list = Controller.GetAllEntries().ToArray();
Debug.WriteLine("List returned in {0}s with a size of {1}", watch.Elapsed.TotalSeconds, list.Count<Lejlighed>());
watch.Restart();
listBox1.Items.AddRange(list);
watch.Stop();
Debug.WriteLine("Added {0} items in {1}s", listBox1.Items.Count, watch.Elapsed.TotalSeconds);
Output is:
List returned in 3.8596527s with a size of 19022
Added 19022 items in 1.9223412s