我正在开发一个代码,其中必须读取数据表单列表框并上传外部文件,请按下名为Start的按钮,并使用此列表框获取错误,如下所示。在C中使用列表框线程#
“System.InvalidOperationException”类型 的未处理的异常发生在System.Windows.Forms.dll中
其他信息:跨线程操作无效:控制 “listBox1中”从其他线程访问比它创建的线程 。
我的代码是如这里在listbox.SetSelected命令如下
private void Start_Click(object sender, EventArgs e)
{
Thread ss = new Thread(Automode);
ss.Start();
}
private void Automode()
{
....
for (int i = 0; i < listBox1.Items.Count; i++)
{
listBox1.SetSelected(i, true);
string pattern = "[gxyzbcmij][-+]?[0-9]*\\.?[0-9]*";
string text = listBox1.Text;
Regex gcode = new Regex(pattern, RegexOptions.IgnoreCase);
MatchCollection code = gcode.Matches(text);
}
.....
}
它给一个异常,如上所示。请提出替代方法来编写它。
与往常一样,将UI代码与业务代码分开......重新设计它,以便线程代码不访问ListBox或任何其他控件。 –