private void DisplayRxFarm(object sender, EventArgs e) { canTrans.FarmsEventArgs args = (canTrans.FarmsEventArgs)e; EventHandler TextBoxDisplayUpdate = delegate //EventHandler是系统自定义的委托 { richTextBoxDisplay.AppendText(args.ToString() + "\r\n"); }; try { Invoke(TextBoxDisplayUpdate); } catch { }; }
private void DisplayTxFarm(object sender, EventArgs e) { canTrans.FarmsEventArgs args = (canTrans.FarmsEventArgs)e; //实例化一个参数类 EventHandler TextBoxDisplayUpdate = delegate //richTextBoxDisplay 主线程创建控件 访问此方法是trans线程 设计跨线程调用 必须用委托 { richTextBoxDisplay.AppendText(args.ToString() + "\r\n"); }; try { Invoke(TextBoxDisplayUpdate); } catch { }; }
void trans_init() { /* 使用事件委托传参 */ driverTrans.EventTxFarms += new EventHandler( (sender1, e1) => { canTrans.FarmsEventArgs args = (canTrans.FarmsEventArgs)e1; EventHandler TextBoxDisplayUpdate = delegate { richTextBoxDisplay.AppendText(args.ToString() + "\r\n"); }; try { Invoke(TextBoxDisplayUpdate); } catch { }; } ); driverTrans.EventRxFarms += new EventHandler( (sender1, e1) => { canTrans.FarmsEventArgs args = (canTrans.FarmsEventArgs)e1; EventHandler TextBoxDisplayUpdate = delegate { richTextBoxDisplay.AppendText(args.ToString() + "\r\n"); }; try { Invoke(TextBoxDisplayUpdate); } catch { }; } ); driverTrans.EventRxMsgs += new EventHandler( (sender1, e1) => { canTrans.RxMsgsEventArgs RxMsgs = (canTrans.RxMsgsEventArgs)e1; autoResponse(StringToHex(RxMsgs.ToString())); } ); driverTrans.EventError += new EventHandler( (sender1, e1) => { canTrans.ErrorEventArgs args = (canTrans.ErrorEventArgs)e1; EventHandler TextBoxDisplayUpdate = delegate { richTextBoxDisplay.AppendText(args.ToString() + "\r\n"); }; try { Invoke(TextBoxDisplayUpdate); } catch { }; } ); driverTrans.CanRead += driver.ReadData; driverTrans.CanWrite += driver.WriteData; }