开发者

Display progress for big ListView add

brief history. I have a little reporting tool that I am creating that uses a backgroundWorker to do the reporting. Works great, the GUI updates nicely and reports what I want it to.

However if I get a lot of results back and then try to update my ListView control the GUI hangs while it is adding all of the ListViewItems. I would like to provide the user with some feedback while this is going on to let them know that the app is still working but can't seem to figure it out.

I have tried to update the ListView from within the BackgroundWorker and of course got the Cross Thread error.

I tried to create a new ListView from within the BackgroundWorker.DoWork and that worked up until I tried to add the ListView to the GUI in the BackgroundWorker.RunWorkerCompleted, and received the Cross Thread error.

I tried to update the ListView from the BackgroundWorker.RunWorkerCompleted and this is where the GUI hangs 开发者_如何学编程(actually turns white), I even created a progress window, and it does the same thing. Once the adding of the ListViewItems is complete the application returns to normal and works fine.

Does anyone have any thoughts?

Thanks Patrick


You should add items to the ListView on the UI thread in RunWorkerCompleted.

You should call the ListView's BeginUpdate method before adding the items, then call EndUpdate afterwards. This should make it substantially faster.

To make it even faster, you should make an array of ListViewItems, then call AddRange with the entire array.
If you do this, you don't need to call BeginUpdate or EndUpdate, since AddRange will call them for you.


Instead of adding all items at once in RunWorkerCompleted add them in batches of 10 or 20 in the ProgressChanged event handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜