c# - 在 BackgroundWorker DoWork 事件中读取 ComboBox 值时出错

标签 c# winforms

当我在 BackgroundWorker DoWork 事件中访问 Form 控件时,它从 DatePicker 而不是从 TextBox 读取值或组合框

错误:

Cross-thread operation not valid: Control 'cmbProgram' accessed from a thread other than the thread it was created on.

enter image description here

代码:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string strDate = dtpDate.Value.ToString();

        string strProgram = cmbProgram.Text;

    }

它如何从 DataPicker 中读取值(在不同的线程中)?

是否有任何解决方法可以从 BackgroundWorker DoWork 事件访问表单控件?

最佳答案

使用:

Dispatcher.Invoke(new Action(() =>
           {
                string strDate = dtpDate.Value.ToString();
                string strProgram = cmbProgram.Text;

           }));

关于c# - 在 BackgroundWorker DoWork 事件中读取 ComboBox 值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10446417/

相关文章:

c# - NUnit DLL 版本不匹配

c# - 对象中的 IEqualityComparer 字符串值

c# - Cache.SetMaxAge 在 IIS 下不工作,在 VS Dev Srv 下工作正常

c# - 使用 winforms 在 c# 中处理跨方法列表

c# - 设置 Hangfire 成功的作业到期属性不起作用

c# - 使用 KeyValuePair 对列表进行排序

c# - Windows窗体中dll的调用方法

C# 窗体 : Automatically Login to a report server using Webbrowser control

c# - 将应用程序带到前台时崩溃并显示 "... stopped working"

c# - 排序后滚动条重置到 treeView 的顶部?