C# 如何从BackgroundWorker正确更新ListView?
我有一个C# 如何从BackgroundWorker正确更新ListView?,c#,.net,multithreading,listview,backgroundworker,C#,.net,Multithreading,Listview,Backgroundworker,我有一个ListView控件,其中包含大约5000项。我正在使用BackgroundWorker搜索ListView控件并在其中查找项目。问题是,当我找到一个项目时,我需要更改它的背景色,并在我的列表视图中滚动到它 我的Backgroundworker代码: private void SearchTrackerList(int column, string text) { foreach (ListViewItem lvi in ListView_DeviceLi
ListView
控件,其中包含大约5000项。我正在使用BackgroundWorker
搜索ListView
控件并在其中查找项目。问题是,当我找到一个项目时,我需要更改它的背景色
,并在我的列表视图中滚动到它
我的Backgroundworker
代码:
private void SearchTrackerList(int column, string text)
{
foreach (ListViewItem lvi in ListView_DeviceList.Items)
{
if (lvi.SubItems[column].Text.Equals(text))
{
this.Invoke(new MethodInvoker(delegate
{
ListView_DeviceList.Items[lvi.Index].EnsureVisible();
lvi.BackColor = Color.Yellow;
}
));
deviceSearchFound = true;
}
}
}
一切都按预期工作,但每次找到匹配项时,用于更新我的ListView
的调用就会触发,这会减慢整个代码的速度。找到项时,更新myListView
控件的正确方法是什么?Winform或WPF?您是否希望这样可以找到并滚动到列表中的多个项?使用调用以避免出现跨线程问题。因此,没有其他真正的选择。break代码>或返回代码>找到匹配项时的循环?紧跟在deviceSearchFound=true之后
@dr.null这对于找到的第一个匹配项是正确的解决方案,但可能有多个匹配项,我需要找到所有匹配项。