C# 后台工作线程c中的跨线程操作无效#
我希望在后台工作程序中运行代码,但遇到以下错误: '跨线程操作无效:从创建控件的线程以外的线程访问控件'MetroCombox1'C# 后台工作线程c中的跨线程操作无效#,c#,combobox,C#,Combobox,我希望在后台工作程序中运行代码,但遇到以下错误: '跨线程操作无效:从创建控件的线程以外的线程访问控件'MetroCombox1' 如何在后台工作程序中使用combobox?您也应该将您的条件置于调用调用下,例如 private void backgroundWorker1\u DoWork(对象发送方,DoWorkEventArgs e) { this.Invoke((MethodInvoker)委托() { 字符串文本=MetroCombox1.text; 如果(MetroCombox1.S
如何在后台工作程序中使用combobox?您也应该将您的条件置于
调用调用下,例如
private void backgroundWorker1\u DoWork(对象发送方,DoWorkEventArgs e)
{
this.Invoke((MethodInvoker)委托()
{
字符串文本=MetroCombox1.text;
如果(MetroCombox1.SelectedItem==“标题”)
{
//这里有一些代码
}
});
}
是关于此问题的现有线程
private void metroRename_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy)
backgroundWorker1.CancelAsync();
backgroundWorker1.RunWorkerAsync(metroComboBox1.Text);
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
this.Invoke((MethodInvoker)delegate ()
{
string text = metroComboBox1.Text;
});
if (metroComboBox1.SelectedItem == "TITLE") //error here
{
//some code here
}
}