同步: 1.定义委托 /// <summary> /// 通讯数据交互委托定义 /// </summary> /// <param name="sender"></param> /// <param name="pArg">通讯交互数据事件改变参数</param> public delegate void CommunicateDataChangedEventHandler(object sender, CommunicateEventArgs pArg); 2.定义事件 /// <summary> /// 通讯接收数据改变事件定义 /// </summary> public CommunicateDataChangedEventHandler OnCommReceiveDataChangedEvent; 3.注册事件 #region【构造函数】 public GCLogic(DeviceModel pDeviceModel, int pBurante, string pComPort) { //注册 OnCommReceiveDataChangedEvent += new CommunicateDataChangedEventHandler(ComLogic_OnCommReceiveDataChangedEvent); } 4.定义事件执行方法 #region【方法:通讯数据改变事件响应方法】 /// <summary> /// 通讯数据改变事件响应方法 /// </summary> /// <param name="sender"></param> /// <param name="pArgs"></param> private void ComLogic_OnCommReceiveDataChangedEvent(object sender, CommunicateEventArgs pArgs) { while (m_RevQueueLock) Thread.Sleep(1); m_RevQueueLock = true; for (int i = 0; i < pArgs.RevData.Length; i++) { m_RevQueue.Enqueue(pArgs.RevData[i]); } m_RevQueueLock = false; } #endregion 5.调用执行方法 if (OnCommReceiveDataChangedEvent != null) { //提交数据 CommunicateEventArgs arg = new CommunicateEventArgs(); arg.RevData = revdata; if (OnCommReceiveDataChangedEvent != null) OnCommReceiveDataChangedEvent(this, arg); }