我有一个主窗口窗体,并在该窗体中我有自定义控件,表示应用程序中的不同屏幕。我想访问此控件的子控件。有什么我没有得到这里...有时我得到这个错误:C#窗体窗体自定义控件跨线程操作
Cross-thread operation not valid: Control 'lblText' accessed from a thread other than the thread it was created on.
但有时一切正常。我不完全明白为什么错误...可能是与外部设备(MEI BillAcceptor)有一个事件(在Form1类内)做了更改控制...所以让我写一个简单的代码...
//user control
public partial class Screen2 : UserControl
{
public void changeValue(string txt)
{
lblText.Text = txt;
}
}
和方法changeValue从当特定事件惜售一个Form1中称为...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BillAcceptor.SomeBillAcceptorEvent +=
new SomeBillAcceptorEventHandler(changeText);
}
private void changeText(object sender, EventArgs args)
{
_screen2.changeValue("some text");
}
}
所以最讨厌的事情是,有时一切实际工作...所以我的问题是“我必须在这里使用Invoke吗?“或者我该如何解决这一方面,对应用变化较小...