我在 UI 线程上有列表视图。我有一些操作要通过后台工作人员的 DoWork 事件处理程序执行,因为它们很耗时。但是我无法访问 DoWork 处理程序中的列表视图项,因为它引发了异常:Cross-thread operation not valid: Control 'bufferedListView1' accessed from a thread other than the thread it was created on.
那么如何在我的 DoWork 事件处理程序中访问我的 bufferedlistview。这是要在 DoWork 中处理的代码:
foreach (ListViewItem item in bufferedListView1.Items)
{
string lname = bufferedListView1.Items[i].Text;
string lno = bufferedListView1.Items[i].SubItems[1].Text;
string gname = bufferedListView1.Items[i].SubItems[2].Text;
string line = lname + "@" + lno + "@" + gname;
if (gname.Contains(sgroup))
{
var m = Regex.Match(line, @"([\w]+)@([+\d]+)@([\w]+)");
if (m.Success)
{
port.WriteLine("AT+CMGS=\"" + m.Groups[2].Value + "\"");
port.Write(txt_msgbox.Text + char.ConvertFromUtf32(26));
Thread.Sleep(4000);
}
sno++;
}
i++;
}