void IMeasurementObserver.Update(Measurement meas)
 {
     if (this.InvokeRequired)
     {
         this.BeginInvoke(new Action(() => InsertOrUpdate(meas)));
     }
     else
     {
         this.InsertOrUpdate(meas);
     }
 }
 public void Update(Measurement meas)
 {
     lock (observers)
     {
         map[meas.Index] = meas;
         foreach (var o in observers)
         {
             o.Update(meas);
         }
     }
 }
 void Add(Measurement meas, string label)
 {
     var text = string.Format("{0} ({1}) - {2} - {3}", label, meas.Index, meas.Value, meas.ShortFlags);
     listBox.Items.Add(text);
 }
 ListViewItem CreateItem(Measurement m)
 {
     string[] text = { m.Index.ToString(), m.Value, m.Flags, m.Timestamp };
     var item = new ListViewItem(text);
     return item;
 }
 void InsertOrUpdate(Measurement meas)
 {
     if (indexToRow.ContainsKey(meas.Index))
     {
         var row = indexToRow[meas.Index];
         this.listView.Items[row] = CreateItem(meas);
     }
     else
     {
         // figure out where to insert
         var entry = indexToRow.FirstOrDefault(kvp => kvp.Key > meas.Index);
         if (entry.Equals(default(KeyValuePair<ushort, int>)))
         {
             var row = listView.Items.Count;
             listView.Items.Add(CreateItem(meas));
             indexToRow[meas.Index] = row;
         }
         else
         {
             listView.Items.Insert(entry.Value, CreateItem(meas));
             indexToRow[meas.Index] = entry.Value;
             var rows = indexToRow.Select(kvp => kvp.Key > meas.Index);
             foreach (var kvp in indexToRow)
             {
                 if (kvp.Key > meas.Index)
                 {
                     indexToRow[kvp.Key] = kvp.Value + 1;
                 }
             }
         }
     }
 }