C# 子线程中访问主线程中的窗体控件
程序员文章站
2022-06-10 18:07:24
...
1)声明委托:
//声明委托
public delegate void EditDisplayCodeRtl(Label displayLbl, string detectResultStr,string codeStr);
2)定义方法
public static void ChangeFindCodeDetectLableInfo(Label lbl, string rstStr ,string codeStr)
{
switch (rstStr)
{
case "01":
lbl.Text = "扫码OK:"+codeStr.Substring(0, 24);
lbl.BackColor = Color.Lime;
break;
case "02":
lbl.Text = "扫码NG:" + codeStr.Substring(0, 24);
lbl.BackColor = Color.Red;
break;
default:
lbl.Text = "无需扫码";
lbl.BackColor = Color.White;
break;
}
3)实例化委托并启用
//实例化委托为e
Global.EditDisplayCodeRtl editDisplayCodeRtlB = new Global.EditDisplayCodeRtl(Global.ChangeFindCodeDetectLableInfo);
//调用this.BeginInvoke()函数,把e和e需要的参数用都好分隔开,传入BeginInvoke()函数
WatchForm.lblDisplayCodeStrB.BeginInvoke(editDisplayCodeRtlB, WatchForm.lblDisplayCodeStrB, ccdCodeResultStrB, ccdCodeNumStrB);
[点击并拖拽以移动]
以上是静态的案列,非静态的将static去掉就Ok