当我在BackgroundWorker DoWork事件中访问表单控件时,它从DatePicker读取值,但不从TextBox或ComboBox读取值。
错误:
Cross-thread operation not valid: Control 'cmbProgram' accessed from a thread other than the thread it was created on.

代码:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
string strDate = dtpDate.Value.ToString();
string strProgram = cmbProgram.Text;
}它如何从DataPicker读取值(在不同的线程中)?
是否存在从BackgroundWorker DoWork事件访问窗体控件的解决方法?
发布于 2012-05-04 09:53:15
使用:
Dispatcher.Invoke(new Action(() =>
{
string strDate = dtpDate.Value.ToString();
string strProgram = cmbProgram.Text;
}));发布于 2012-05-04 09:49:22
不能从其他线程访问控件。解决该问题的通常方法是从UI线程读取当前值,然后将该值传递给第二个线程(或BackgroundWorker)。
可以通过将控件类上的CheckForIllegalCrossThreadCalls设置为false来禁用检查,但请注意不要这样做。
发布于 2012-05-04 09:53:38
你可以通过它作为一个论点。例如:
backgroundworker1.RunWorkerAsync(comboBox1.SelectedItem.ToString());获取doWork中的内容
string Item = e.Argument.ToString();https://stackoverflow.com/questions/10446417
复制相似问题