尝试在 Backgroundworker 的 IF 语句中获取组合框的值时遇到一些问题。当我尝试运行这段代码时
if (KondisiSaldo.SelectedItem == "Kurang dari...")
{
view.RowFilter = string.Format("[Saldo] < '{0}'", thresholdcas);
view2.RowFilter = string.Format("[Saldo] > '{0}'", thresholdcas);
this.Invoke(new MethodInvoker(delegate
{
ViewDataSaldoGV.DataSource = view;
SaldoUnscheduleGV.DataSource = view2;
}));
}
错误说
Cross-thread operation not valid: Control 'KondisiSaldo' accessed from a thread other than the thread it was created on.
谁能帮帮我?
最佳答案
您正在非 GUI 线程中访问 KondisiSaldo
。将 KondisiSaldo
放在 Invoke block 中以在 GUI 线程上访问它,就像您对 view
和 view2
控件所做的那样。
this.Invoke(new MethodInvoker(delegate
{
if (KondisiSaldo.SelectedItem == "Kurang dari...")
{
view.RowFilter = string.Format("[Saldo] < '{0}'", thresholdcas);
view2.RowFilter = string.Format("[Saldo] > '{0}'", thresholdcas);
ViewDataSaldoGV.DataSource = view;
SaldoUnscheduleGV.DataSource = view2;
}
}));
您可能需要调整您的状况。
关于c# - “Cross-thread operation is not valid” 尝试在 Backgroundworker 中获取 comboBox 的值时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41950173/