如何以最小的延迟向listctrl添加大量项目? [英] How add huge number of items to listctrl with minimum delay?

查看:91
本文介绍了如何以最小的延迟向listctrl添加大量项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个List控件,应该向其添加大约280000个项目,然后显示。对我来说大约需要4分钟,我的UI会在此期间禁用。



如何减少延迟时间?



我尝试过:



I have a List control which about 280000 items should be added to that and then display. it takes about 4 minutes for me and my UI goes disable during the time.

how can I reduce the Delay time?

What I have tried:

pTable = new CListCtrl;
CString strtemp;
for (unsigned int i = 1; i <= size; i++)
{
    pTable->InsertItem(i, strtemp);
}

推荐答案

谁将阅读(并滚动)280,000项?



解决方案是使用虚拟列表控件来处理实际可见的项目。请参阅虚拟列表控件| Microsoft Docs [ ^ ]。



对于非虚拟列表控件,您可以(并且应该始终使用较小的列表)在修改列表时禁用屏幕更新:

Who will read (and scroll through) 280,000 items?

A solution would be using a virtual list control that handles only the items actually visible. See Virtual List Controls | Microsoft Docs[^].

For a non virtual list control you can (and should always even with smaller lists) disable the screen update while modifying the list:
pTable->SetRedraw(FALSE);

// Modify list here

pTable->SetRedraw(TRUE);

// Invalidate the entire list
pTable->Invalidate();
// Force painting
pTable->UpdateWindow();







事先知道项目数量,最初使用 SetItemCount()。即使不提前知道计数,最终确定和设置它也可能更快。



为了进一步改进,尽可能快地使循环内的代码(简单)。这包括避免在循环中分配内存的任何函数。使用例如preallocated CString s或使用max在循环外定义的纯文本缓冲区。可能的字符串长度如果可能的话,还要避免任何字符串格式化功能。

[/ EDIT]




When knowing the number of items in advance, use SetItemCount() initially. Even when not knowing the count in advance, it might be finally faster to determine and set it.

For further improvements make the code inside the loop as fast as possible (simple). That includes avoiding any function that allocates memory in the loop. Use for example preallocated CStrings or plain text buffers defined outside the loop using the max. possible string length. If possible, avoid also any string formatting function.
[/EDIT]


您是否考虑过分页或无限滚动?可以在不同的线程中创建/检索项目,但更新控件总是阻止UI线程,除非控件支持某种虚拟化。最好的方法是按部分添加项目(例如一次添加200个项目),但您应该决定何时添加特定部分:一种方法是分页。当用户滚动到底部时(无限滚动),您还可以添加下一批项目。
Have you considered paging or infinite scrolling? Items may be created/retrieved in a different thread but updating a control always blocks UI thread unless the control supports some kind of virtualization. The best approach is add items by parts (for example 200 items at a time) but you should decide when to add specific part: the one approach is paging. You can also add the next batch o of items when user scrolls to the bottom (infinite scrolling).


这篇关于如何以最小的延迟向listctrl添加大量项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆