private delegate void listviewClient(ListViewItem item); //声明委托 private void button1_Click(object sender, EventArgs e) { Thread uploadThread = new Thread(new ParameterizedThreadStart(upLoad)); uploadThread.IsBackground = true; uploadThread.Start(); } private void upLoad(object objdate) { ListViewItem lviheader = new ListViewItem(); lviheader.Text = "12345"; if (listView1.InvokeRequired) { listviewClient lv1 = new listviewClient(listViewAdd); //因为需要调用住窗体的 listView 控件,所以使在线程中使用委托 Invoke(lv1, lviheader); } else { listView1.Items.Add(lviheader); } lviheader.Text = "54321"; //当Invoke()执行完以后,再改变lviheader.Text的值,就出现异常 } private void listViewAdd(ListViewItem item) { listView1.Items.Add(item); }
当Invoke()执行完以后,再改变lviheader.Text的值,就出现异常:
“线程间操作无效: 从不是创建控件“listView1”的线程访问它。”
求高人指点并解释一下,不胜感激!