private void WriteLinearizationItems(List <LinearizationItem> lis) { FrmProcessing frm = new FrmProcessing(); Action action = delegate() { try { for (int i = 0; i < lis.Count; i += 2) { LinearizationItem[] items = new LinearizationItem[2] { lis[i], lis[i + 1] }; bool ret = HartDevice.WriteLinearizationItems(items, (byte)(i / 2)); frm.ShowProgress(string.Empty, (decimal)(i + 2) / lis.Count); Thread.Sleep(100); } frm.ShowProgress(string.Empty, 1); } catch (ThreadAbortException) { } catch (Exception) { } }; Thread t = new Thread(new ThreadStart(action)); t.IsBackground = true; t.Start(); if (frm.ShowDialog() != DialogResult.OK) { t.Abort(); } }
private void ReadLinearizationItems() { FrmProcessing frm = new FrmProcessing(); Action a = delegate() { try { int maxAD = 70000; if (HartDevice != null && HartDevice.IsConnected) { for (int i = 1; i <= 10; i++) { LinearizationItem li = null; for (int j = 0; j < 4; j++) //由于有时会读不到数据,所以每组数据在读取失败时会重新偿试读三次 { li = HartDevice.ReadLinearizationItem((byte)i); if (li != null) { break; } Thread.Sleep(500); } if (li != null && li.SensorAD == maxAD) { break; //后面的都是无效的参数了,不用再继续获取 } this.Invoke((Action)(() => { (this.Controls["txtP" + i.ToString()] as TextBox).Text = li != null ? li.SensorValue.ToString() : null; (this.Controls["txtAD" + i.ToString()] as TextBox).Text = li != null ? li.SensorAD.ToString() : null; })); frm.ShowProgress(string.Empty, (decimal)i / 10); } } frm.ShowProgress(string.Empty, 1); } catch (ThreadAbortException) { } catch (Exception) { } }; Thread t = new Thread(new ThreadStart(a)); t.IsBackground = true; t.Start(); if (frm.ShowDialog() != DialogResult.OK) { t.Abort(); } }