我想从另一个线程使用foreach(objectListView1.Items中的ListViewItem项目),但是我不知道如何使用。
我的代码:
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
}
}));
}
关于c# - 从另一个线程foreach listview项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21443843/