title: Updating UI in realtime
body: <p>I have to create a WPF UI, which subsribes to real time Fx Rate(Currency + rate) updates and displays them in a grid (roughly 1000 updates per second, which means each row in the grid could get updated <strong>upto</strong> 1000 times per second).The grid would have atleast 50 rows at any point in time.</p>
  
  <p>Towards this, I have created a Viewmodel which subscribes to the update events, and store those updates inside a concurrent dictionary with key as symbol and value as a RateViewModel object. Then I have another observable collection which has all those rateviewmodel objects, and bind that to a grid.</p>
  
  <p>Code:</p>
  
  <pre><code>public class MyViewModel
      {
          private readonly IRatesService ratesService;
  
          private readonly ConcurrentDictionary&lt;string, RateViewModel&gt; rateDictionary;
          private object _locker = new object();
  
          public MyViewModel(IRatesService ratesService)
          {
              this.ratesService = ratesService;
              this.ratesService.OnUpdate += OnUpdate;
              rateDictionary = new ConcurrentDictionary&lt;string, RateViewModel&gt;();
              RateViewModels = new ObservableCollection&lt;RateViewModel&gt;();            
          }
  
          private void OnUpdate(object sender, RateUpdateEventArgs e)
          {
              RateViewModel exisistingRate;
              if (!rateDictionary.TryGetValue(e.Update.Currency, out exisistingRate))
              {
                  exisistingRate = new RateViewModel(new Rate(e.Update.Currency, e.Update.Rate));
                  rateDictionary.TryAdd(e.Update.Currency, exisistingRate);                
                  return;
              }
  
              lock (_locker)
              {
                  exisistingRate.UpdateRate(e.Update.Rate);                
              }
  
              Application.Current.Dispatcher.BeginInvoke(new Action(() =&gt; SearchAndUpdate(exisistingRate)));
          }
  
          public ObservableCollection&lt;RateViewModel&gt; RateViewModels { get; set; }
  
          private void SearchAndUpdate(RateViewModel rateViewModel)
          {
              //Equals is based on Currency
              if (!RateViewModels.Contains(rateViewModel))
              {
                  RateViewModels.Add(rateViewModel);
                  return;
              }
  
              var index = RateViewModels.IndexOf(rateViewModel);
              RateViewModels[index] = rateViewModel;
          }      
      }
  </code></pre>
  
  <p>I have 4 questions over this:</p>
  
  <ul>
  <li><p>Is there a way I can eliminate the ObservableCollection, as it's leading to 2 different datastructures storing the same items - but still have my updates relayed to the UI?</p></li>
  <li><p>I have used Concurrent Dictionary, which leads to locking the whole update operation. Is there any other clever way of handling this rather than locking the whole dicitionary or for that matter any datastructure?</p></li>
  <li><p>My UpdateRate method also locks - all my properties on my RateviewModel is read only except the price, as this is getting updated. Is there a way to make this atomic, please note that the price is coming in as a double.</p></li>
  <li><p>Is there a way I can optimize the SearchAndUpdate method, this is kind of related to 1st. At the moment I believe it's an O(n)operation.</p></li>
  </ul>
  
  <p>Using <strong>.NET 4.0</strong> and have omitted INPC for brevity.</p>
  
  <p>*<em>EDIT:</em>*Could you please help me in rewriting this in a better manner taking all the 4 points into account? Psuedocode will do.</p>
  
  <p>Thanks,
  -Mike</p>