gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:26:20 25 4
gpt4 key购买 nike

当我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com