protected virtual void OnUpdateDataGridView(UpdateDataGridViewCellsEventArgs e) { //EventHandler<UpdateDataGridViewCellsEventArgs> handler = UpdateDataGridView; //if (handler != null) //{ // handler(this, e); //} UpdateDataGridView?.Invoke(this, e); }
private void BackgroundWorkerReceiveData_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker bgWorker = sender as BackgroundWorker; IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); while (true) { try { //IPEndPoint object will allow us to read datagrams sent from any source. // Blocks until a message returns on this socket from a remote host. Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint); string stamp = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"); if (receiveBytes.Length != 44) { continue; } StringBuilder sb = new StringBuilder(1024); sb.Append(stamp + ","); int startIndex = 4; List <UpdateArgs> args = new List <UpdateArgs>(); for (int i = 0; i < NumberOfChannels; i++) { if (!strainChannels.ContainsKey(i + 1)) { continue; } //digit double digit = CalculateDigit(receiveBytes, startIndex, i); //温度 double temperature = CalculateTempreture(receiveBytes, startIndex, i); StrainChannel sc = strainChannels[i + 1]; double strain = sc.CalculateStrain(digit, temperature); bool isSuccess = true; if (digit < 0.001 || Math.Abs(temperature) < 0.001) { log.Warn(Tag + "Sensor Error: digit=" + digit.ToString() + "temperature=" + temperature.ToString()); isSuccess = false; } string strainVal = ""; string strainState = ""; if (!isSuccess) { strainVal = "频率或者温度为零无法计算"; strainState = "failed"; } else { strainVal = strain.ToString(); strainState = "Success"; } UpdateArgs arg = new UpdateArgs(); arg.index = sc.gridIndex; arg.digit = digit; arg.stamp = stamp; arg.temp = temperature; arg.strain = strainVal; arg.state = strainState; args.Add(arg); sb.Append(digit.ToString() + "," + temperature.ToString() + ","); } UpdateDataGridViewCellsEventArgs eventArgs = new UpdateDataGridViewCellsEventArgs(); eventArgs.args = args; OnUpdateDataGridView(eventArgs); sb.Remove(sb.Length - 1, 1); sb.Append("\r\n"); AppendRecord(sb, this.config.remoteIP); if (bgWorker.CancellationPending == true) { e.Cancel = true; break; } } catch (Exception ex) { log.Error(Tag, ex); if (bgWorker.CancellationPending == true) { e.Cancel = true; break; } } if (bgWorker.CancellationPending == true) { e.Cancel = true; break; } } }