C# 来自另一个线程的foreach listview项
我想从另一个线程使用foreach(objectListView1.Items中的ListViewItem项),但我不知道如何使用 我的代码:C# 来自另一个线程的foreach listview项,c#,multithreading,winforms,listview,C#,Multithreading,Winforms,Listview,我想从另一个线程使用foreach(objectListView1.Items中的ListViewItem项),但我不知道如何使用 我的代码: void createMoviesXML() { foreach (ListViewItem item in objectListView1.Invoke(new Action(() => objectListView1.Items)) // I know it's wrong
void createMoviesXML()
{
foreach (ListViewItem item in objectListView1.Invoke(new Action(() => objectListView1.Items)) // I know it's wrong
{
if (Regex.IsMatch(item.SubItems[0].Text, @"&"))
{
item.SubItems[0].Text = Regex.Replace(item.SubItems[0].Text, @"&", "&");
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
Thread thr1 = new Thread(new ThreadStart(createMoviesXML));
thr1.Start();
}
您需要在UI线程上执行整个循环,因为您要接触多个控件
void createMoviesXML()
{
objectListView1.Invoke(new Action(() =>
{
foreach (var item in objectListView1.Items)
{
// Do stuff
}
}));
}
这么多问题。。第一个问题是为什么要使用正则表达式来匹配基本字符串?我不知道。我认为这是最好的办法。我要扩展这个正则表达式。当然,这将需要从另一个线程foreach。