Populate ListBox with a IEnumrable on another thread (winforms)

前端 未结 1 867
余生分开走
余生分开走 2020-12-22 14:08

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 forea

相关标签:
1条回答
  • 2020-12-22 14:19

    You don't need to populate the ListBox from in another thread. If you use a correct way to populate it, populating 10000 items takes a short time (for me 200-300 ms).

    The part that you may want to put in another thread is loading data not adding data to ListBox.

    To add items to ListBox it's enough to use AddRange:

    this.listBox1.AddRange(array);
    

    Which is equivalent to using below code. First call BeginUpdate method of ListBox and then use a loop to Add items to Items collection and at last call EndUpdate:

    this.listBox1.BeginUpdate();
    foreach (var item in array)
    {
        this.listBox1.Items.Add(item);
    }
    this.listBox1.EndUpdate();
    

    Take a look at source code of AddRange method.

    0 讨论(0)
提交回复
热议问题