C# 从多个线程快速添加到ListView

C# 从多个线程快速添加到ListView,c#,listview,C#,Listview,在向我的列表视图添加项目时,我使用以下方法 我已经确保了列表视图的双缓冲,并尝试优化我能想到的一切——但无论我做什么,当快速添加项目时,UI都很缓慢 我有这个问题已经有相当长的一段时间了,我四处寻找,试图找到一个解决方案,但每次都放弃了,因为我只是无法解决它。这次我希望能解决这个问题 我在想也许可以用一些定制的解决方案?有什么好的能处理“速度”的吗?或者我可以用我当前的代码做些什么 方法: private void AddNewItemToListView(string gPR, string

在向我的
列表视图添加项目时,我使用以下方法

我已经确保了
列表视图
的双缓冲,并尝试优化我能想到的一切——但无论我做什么,当快速添加项目时,UI都很缓慢

我有这个问题已经有相当长的一段时间了,我四处寻找,试图找到一个解决方案,但每次都放弃了,因为我只是无法解决它。这次我希望能解决这个问题

我在想也许可以用一些定制的解决方案?有什么好的能处理“速度”的吗?或者我可以用我当前的代码做些什么

方法:

private void AddNewItemToListView(string gPR, string rank, string category, string name, string url, string email, string address, string phone, string metadesc, string metakeywords, string mobile, string numbofreviews, string rating, string facebook, string twitter, string googleplus, string linkedin, string sitemap, string siteage, string backlinks, string trafficvalue)
{
    Invoke(new MethodInvoker(
        delegate
            {
                string[] row1 = { url, urlSec, address, phone, metadesc, metakeywords, mob, REV, RT, gPR, FB, TW, googleplus, LI, ST, SA, BL, TV };
                ListViewItem item = new ListViewItem();

                flatListView1.Items.Add(name).SubItems.AddRange(row1);                               
            }
        ));
}

您可以不直接添加到UI,而是添加到一个列表中,该列表会慢慢地被消耗,并以每秒x个项目的速度添加到UI中。我很快在下面写了一个松散的示例,但您可以在这里阅读更多内容:

private BlockingCollection队列;
公开作废开始()
{
队列=新建BlockingCollection();
Task.Factory.StartNew(()=>{
而(!queue.IsCompleted)
{ 
var item=queue.Take();
//添加到listview并控制速度
} 
});
Start.Whatever.products.Items();
//将所有项目添加到队列时。
queue.completedAdded();
}
私有void AddnewItemToListView(…)
{
var行=。。。;
queue.Add(行);
}

您可以添加到一个列表中,而不是直接添加到UI中,该列表会慢慢消耗,并以每秒x个项目的速度添加到UI中。我很快在下面写了一个松散的示例,但您可以在这里阅读更多内容:

private BlockingCollection队列;
公开作废开始()
{
队列=新建BlockingCollection();
Task.Factory.StartNew(()=>{
而(!queue.IsCompleted)
{ 
var item=queue.Take();
//添加到listview并控制速度
} 
});
Start.Whatever.products.Items();
//将所有项目添加到队列时。
queue.completedAdded();
}
私有void AddnewItemToListView(…)
{
var行=。。。;
queue.Add(行);
}

能否在工作开始时使用ListView.SuspendLayout()方法,然后在工作结束时调用ListView.ResumeLayout()?我想这会使事情加快很多。您也可以尝试定期恢复,以获得一些反馈。例如,在指定位置插入以下代码:

// Start of work
flatListView1.SuspendLayout();

// Below code inside your delegate

flatListView1.Items.Add(name).SubItems.AddRange(row1);   

if ((flatListView1.Items.Count % 1000) == 0)
{
    // Force a refresh of the list
    flatListView1.ResumeLayout();
    // Turn it off again
    flatListView1.SuspendLayout();         
}

// End of code inside delegate

// Resume layout when adding is finished

flatListView1.ResumeLayout();

您能在工作开始时使用ListView.SuspendLayout()方法,然后在工作结束时调用ListView.ResumeLayout()吗?我想这会使事情加快很多。您也可以尝试定期恢复,以获得一些反馈。例如,在指定位置插入以下代码:

// Start of work
flatListView1.SuspendLayout();

// Below code inside your delegate

flatListView1.Items.Add(name).SubItems.AddRange(row1);   

if ((flatListView1.Items.Count % 1000) == 0)
{
    // Force a refresh of the list
    flatListView1.ResumeLayout();
    // Turn it off again
    flatListView1.SuspendLayout();         
}

// End of code inside delegate

// Resume layout when adding is finished

flatListView1.ResumeLayout();


嗯,听起来很聪明。。我如何在代码中实现这一点?如果您从多个线程写入,请确保使用
ConcurrentBag
。@Jacqueline-UI优化不是一项小任务或无关紧要的任务。在问“如何做”之前,你可能想更彻底地研究这个话题:)@Cyborgx37年我开始意识到:)我的程序运行了500个线程,所以当向LWHmm写入时UI会变得非常缓慢,听起来非常聪明。。我如何在代码中实现这一点?如果您从多个线程写入,请确保使用
ConcurrentBag
。@Jacqueline-UI优化不是一项小任务或无关紧要的任务。在问“如何做”之前,你可能想更彻底地研究这个话题:@Cyborgx37年我开始意识到:)我的程序运行了500个线程,因此在写入LW时UI变得非常缓慢如果你将
Invoke
更改为
BeginInvoke
,速度会提高吗如果你将
Invoke
更改为
BeginInvoke
,速度会提高吗,这只会阻止ListView在每次添加时更新其UI,而不是每1000项刷新一次。谢谢Daniel!让我省去了这个头疼:)真高兴听到你这么说!这些方法继承自
System.Windows.Forms.Control
,因此它们也可用于包含大量数据的其他控件,如TreeView、ListBox等。这真是太棒了!我在我所有的ListView控件上都实现了这一点,这让我的应用更加专业:)+++对你来说DanielNo,它只会阻止ListView在每次添加时更新其UI,而是每1000个项目刷新一次。谢谢Daniel!让我省去了这个头疼:)真高兴听到你这么说!这些方法继承自
System.Windows.Forms.Control
,因此它们也可用于包含大量数据的其他控件,如TreeView、ListBox等。这真是太棒了!我在我所有的ListView控件上都实现了这一点,这让我的应用程序更加专业:)+++丹尼尔