0

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

问题陈述与此线程完全相同 -使用 BlockingCollection 更新 ObservableCollection

我不想使用响应式扩展,但想要传统的 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);
                }
            }
        }
    }
}
4

1 回答 1

2

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

如果您希望它持久,请将您的替换foreach为:

foreach (StockModel sm in tasks.GetConsumingEnumerable())

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

不过,我对您如何将内容放入阻塞集合中有些困惑。您的代码段不太清楚。如果您遇到问题,请编辑您的问题并解释问题所在。代码中的注释handlermethod非常神秘。

于 2013-07-10T21:45:35.263 回答