title: Removing Elements from BlockingCollection and populating Observable Collection
body: <p>I have a use-case wherein i want to insert and remove custom object (Stocks) from a blocking collection (larger picture being producer consumer queue). </p>
  
  <p>The problem statement is precisely similar to this thread - <a href="https://stackoverflow.com/questions/13234103/update-an-observablecollection-with-a-blockingcollection">update an ObservableCollection with a BlockingCollection</a></p>
  
  <p>I dont want to use reactive extensions but want a traditional C# way to do this logic (this is a hard requirement unfortunately and completely understand the implications). My snippet of code is here</p>
  
  <p>MainWindowViewModel.cs</p>
  
  <pre><code>public class MainWindow_VM : ViewModelBase
  {
      public ObservableCollection&lt;StockModel&gt; stocks { get; set; }
      private readonly Dispatcher currentDispatcher;
      private BlockingCollection&lt;StockModel&gt; tasks = new BlockingCollection&lt;StockModel&gt;();
      #endregion
  
  // All other standard ViewModel logic - Constructor, Command etc
  
      private void handlermethod(object sender, MarketDataEventArgs e)
      {
          Task.Factory.StartNew(AddUpdateObservableCollection);
  
          // Below thought process (maybe wrong) - How do i add the value to the BlockingCollection through a thread considering I have a ProducerConsumer class standard implementation (which has Enqueue and Dequeue Methods)
          using (ProducerConsumerQueue q = new ProducerConsumerQueue())
          {
                foreach (Stock s in e.updatedstock)
                {
                    StockModel sm = new StockModel();
                    sm.Symbol = s.Symbol;
                    sm.Bidprice = s.Bidprice;
  
                    q.EnqueueTask(s); 
                 }
      }
  
      private void AddUpdateObservableCollection()
      {
          //Signalling mechanism still missing - when Stock comes into BlockingCollection - then this will start draining.
         // Also have to take care of Dispatcher stuff since you can only update ObservableCollection through Dispatcher
  
          foreach (StockModel sm in tasks)
          {
              if (sm != null)
              {
                  if (stocks.Any(x =&gt; x.Symbol == sm.Symbol))
                  {
                      var found = stocks.FirstOrDefault(x =&gt; x.Symbol == sm.Symbol);
                      int i = stocks.IndexOf(found);
                      stocks[i] = sm;
                  }
                  else
                  {
                      stocks.Add(sm);
                  }
              }
          }
      }
  }
  </code></pre>