Populating a listview from another thread

后端 未结 5 644
旧时难觅i
旧时难觅i 2021-01-21 21:01

I\'m trying to populate a listview from another class, but I\'m geting this error: \" Cross-thread operation not valid: Control \'listView1\' accessed from a thread other than t

5条回答
  •  礼貌的吻别
    2021-01-21 21:38

    This is a reasonable thing to do, because often in application you want ListView updates etc to go on without holding up your code.

    I have done the message systems between user controls containing the control/s I want to update in the back ground and it can get quite messy because you end up having to message/event for a lot more than just the fill/updates, messy code is buggy code, so I tried other ways.

    There is a nice neat way, the slow part of the ListView fill/updates is generally in the creation of the ListViewItems, and you can fully prepare those in your own thread.

    So now, for this sort of applications (With Fill or update ListView where I do not need to wait for it to be ready before my code can continue), My seperate thread Creates/prepares the ListViewItems then adding the prepared items to the ListView when the thread is done is very quick, so the final ListView update can be done on a user event hardly noticeable to the user. Add to this 'Only add those you can see' and it really is instantaneous. With a couple of extra rows so when scrolling starts you can add a couple more. (You might have noticed youtube/facebook/windows picture browser all do it this way). Since in our case we have already prepared the ListViewItems, adding them to the list is very straight forward.

提交回复
热议问题