private void refrashStallList(string info) { //判断该方法是否被主线程调用,也就是创建labMessage1控件的线程,当控件的InvokeRequired属性为ture时,说明是被主线程以外的线程调用。如果不加判断,会造成异常 if (this.tc_carport.InvokeRequired) { //再次创建一个DelegateUpdateUI类的对象 DelegateUpdateUI updateUI = new DelegateUpdateUI(); //为新对象的mainThread对象搭载方法 updateUI.mainThread = new DelegateUpdateUI.updateDelegate(refrashStallList); //this指窗体,在这调用窗体的Invoke方法,也就是用窗体的创建线程来执行mainThread对象委托的方法,再加上需要的参数(i) this.Invoke(updateUI.mainThread, new object[] { info }); } else { if (info != null) { // playMusic(); switch (info) { case "1": stall1.Image = Properties.Resources.empty; status_notify.Text = "当前通知:1号车位驶出车辆"; break; case "2": stall2.Image = Properties.Resources.empty; status_notify.Text = "当前通知:2号车位驶出车辆"; break; case "3": stall3.Image = Properties.Resources.empty; status_notify.Text = "当前通知:3号车位驶出车辆"; break; case "A": stall1.Image = Properties.Resources.car; status_notify.Text = "当前通知:1号车位驶入车辆"; break; case "B": stall2.Image = Properties.Resources.car; status_notify.Text = "当前通知:2号车位驶入车辆"; break; case "C": stall3.Image = Properties.Resources.car; status_notify.Text = "当前通知:3号车位驶入车辆"; break; } updateStallInfo(info); } } }
private void createDelegate() { updateDelegate = new DelegateUpdateUI(); updateDelegate.mainThread = new DelegateUpdateUI.updateDelegate(refrashStallList); }