Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从BackgroundWorker正确更新ListView?_C#_.net_Multithreading_Listview_Backgroundworker - Fatal编程技术网

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
的调用就会触发,这会减慢整个代码的速度。找到项时,更新my
ListView
控件的正确方法是什么?

Winform或WPF?您是否希望这样可以找到并滚动到列表中的多个项?使用调用以避免出现跨线程问题。因此,没有其他真正的选择。
break
返回找到匹配项时的循环?紧跟在
deviceSearchFound=true之后
@dr.null这对于找到的第一个匹配项是正确的解决方案,但可能有多个匹配项,我需要找到所有匹配项。