如何从比它创建的线程以外的线程读取组合框? [英] How to read combobox from a thread other than the thread it was created on?
问题描述
我试图从比它的创建上,但我得到了错误的线程以外的线程读取combobox.Text:
I am trying to read a combobox.Text from a thread other than the thread it was created on but I am getting the error:
型
未处理的异常'System.InvalidOperationException'
发生在System.Windows.Forms.dll中
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
更多信息:跨线程
操作无效:控制
'levelsComboBox从比它是
创建的线程以外的
线程访问
Additional information: Cross-thread operation not valid: Control 'levelsComboBox' accessed from a thread other than the thread it was created on.
我用.Invoke之前,但只设置属性,我怎么可以用它来阅读combobox.Text?由于.Invoke返回void,我需要一个字符串。 ?还是有另一种方式做到这一点,而不调用
I have used .Invoke before but only to set properties, how can I use it to read combobox.Text? Because .Invoke returns void and I need a string. Or is there another way to do it without the Invoke?
推荐答案
您可以做到这一点是这样的:
You can do it like this:
this.Invoke((MethodInvoker)delegate()
{
text = combobox.Text;
});
这篇关于如何从比它创建的线程以外的线程读取组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!