从BlockingCollection中删除元素并填充Observable Collection

时间:2013-07-10 20:46:49

标签: c# wpf observablecollection blockingcollection

我有一个用例,其中我想从阻塞集合中插入和删除自定义对象(Stocks)(较大的图片是生产者消费者队列)。

问题陈述与此线程完全相似 - update an ObservableCollection with a BlockingCollection

我不想使用反应式扩展,但希望采用传统的C#方式来实现这一逻辑(不幸的是,这是一个很难的要求并完全理解其含义)。我的代码片段在这里

MainWindowViewModel.cs

public class MainWindow_VM : ViewModelBase
{
    public ObservableCollection<StockModel> stocks { get; set; }
    private readonly Dispatcher currentDispatcher;
    private BlockingCollection<StockModel> tasks = new BlockingCollection<StockModel>();
    #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 => x.Symbol == sm.Symbol))
                {
                    var found = stocks.FirstOrDefault(x => x.Symbol == sm.Symbol);
                    int i = stocks.IndexOf(found);
                    stocks[i] = sm;
                }
                else
                {
                    stocks.Add(sm);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

问题是您的AddUpdateObservableCollection方法看到tasks集合为空,然后退出。

如果您想让它保持持久性,请将foreach替换为:

foreach (StockModel sm in tasks.GetConsumingEnumerable())

GetConsumingEnumerable获取的枚举器将阻止等待添加到队列中的项目,并将继续阻塞,直到另一个线程将该集合标记为完成添加。因此,当您完成向队列中添加内容并希望退出AddUpdateObservableCollection方法时,只需调用tasks.CompleteAdding()即可。循环将清空队列,看到没有更多项目即将到来(因为IsAddingCompleted属性为true),然后退出。

但是,我对你如何进入阻塞系列感到有些困惑。您的代码段不太清楚。如果您遇到问题,请编辑您的问题并解释问题所在。 <{1}}代码中的注释非常含糊。