显示大型 ListView 添加的进度

发布于 2024-08-16 03:53:47 字数 539 浏览 4 评论 0 原文

简史。我正在创建一个小报告工具,它使用 backgroundWorker 来进行报告。效果很好,GUI 更新得很好并报告了我想要的内容。

但是,如果我返回大量结果,然后尝试更新我的 ListView 控件,则 GUI 在添加所有 ListViewItems 时会挂起。我想在这过程中向用户提供一些反馈,让他们知道该应用程序仍在运行,但似乎无法弄清楚。

我尝试从BackgroundWorker 中更新ListView,当然得到了跨线程错误。

我尝试从BackgroundWorker.DoWork 中创建一个新的ListView,直到我尝试将ListView 添加到BackgroundWorker.RunWorkerCompleted 中的GUI 并收到跨线程错误为止。

我尝试从BackgroundWorker.RunWorkerCompleted更新ListView,这就是GUI挂起的地方(实际上变成白色),我什至创建了一个进度窗口,它做了同样的事情。添加 ListViewItems 完成后,应用程序将恢复正常并正常工作。

有人有什么想法吗?

谢谢 帕特里克

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无所的.畏惧 2024-08-23 03:53:47

您应该将项目添加到 RunWorkerCompleted 中 UI 线程上的 ListView。

您应该调用 ListView 的 BeginUpdate添加项目之前的方法,然后调用 EndUpdate。这应该会大大加快速度。

为了使其更快,您应该创建一个 ListViewItems 数组,然后调用 AddRange 包含整个数组。
如果这样做,则不需要调用 BeginUpdate 或 EndUpdate,因为 AddRange 会为您调用它们。

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.

美男兮 2024-08-23 03:53:47

不要在 RunWorkerCompleted 中一次性添加所有项目,而是在 ProgressChanged 事件处理程序中批量添加 10 或 20 个项目。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文