2

全部 -

我目前有一个端到端工作的 POC WPF 项目。该应用程序模拟通过库(发布者)发布的实时市场数据,我的 WPF 客户端是订阅者(具有处理程序方法)。它使用自定义事件来发布数据。

我的问题是这样的:

1)我想实现生产者消费者 - 所以我的处理程序不会直接将数据拉入 Observable Collection。

2) 我确切地知道如何实现生产者/消费者 C# 片段 ( http://msdn.microsoft.com/en-us/library/hh228601.aspx ),但想更多地了解这将如何适合我当前的架构。这是一个图表

3)任何人都可以用代码方法,链接等帮助我吗?

在此处输入图像描述

MainWindowViewModel.cs

public class MainWindow_VM : ViewModelBase
{
    #region Properties
    public myCommand SbmtCmd { get; set; }
    public ObservableCollection<StockModel> stocks { get; set; }
    #endregion

    #region Fields
    private readonly Dispatcher currentDispatcher;
    #endregion

    public MainWindow_VM()
    {
        SbmtCmd = new myCommand(mySbmtCmdExecute, myCanSbmtCmdExecute);
        currentDispatcher = Dispatcher.CurrentDispatcher;
        stocks = new ObservableCollection<StockModel>();
    }

    private void mySbmtCmdExecute(object parameter)
    {
        MarketDataProvider p = new MarketDataProvider();
        p.OnMarketData += new EventHandler<MarketDataEventArgs>(handlermethod);     
        p.GenerateMarketData();
    }

    private bool myCanSbmtCmdExecute(object parameter)
    {
        return true;
    }


    // Subscriber method which will be called when the publisher raises an event 

    private void handlermethod(object sender, MarketDataEventArgs e)
    {
        foreach (Stock s in e.updatedstk)
        {
            StockModel sm = new StockModel();

            sm.symbol = s.symbol;
            sm.bidprice = s.bidprice;
            sm.askprice = s.askprice;
            sm.lastprice = s.lastprice;

            currentDispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate()
            {
                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

2 回答 2

1

我创建了一个名为ReactiveTables的 UI 工具包,它允许您创建可以使用计算列连接、过滤、扩展然后绑定到 WPF 控件的活动表。这些表公开了一个 IObservable 接口,并且专为提高性能而设计。

您可以将表直接连接到您的生产者/消费者实现,然后将它们绑定到您的视图。表格将通知单个单元格的更改,并且有一个帮助程序类用于将其转换为 INotifyPropertyChanged 事件。

在接收端,有一些类可以限制对 UI 表的更新,并将数据线程编组到 UI 线程。

于 2013-09-28T14:15:52.463 回答
1

I have done some projects with market feeds and your chart looks fine conceptually. To avoid scalability issues, or to design proactively against scalability issues, you can consider making your producer/consumer box have multiple instances to accommodate multiple feeds and/or multiple instruments within the feed. If, for example, a given market becomes densely volatile, you don't want all the other instruments starved for data.

Also, some people like to switch feeds for a given instrument based upon arbitrary criteria, like getting YEN from London until the gold fix, and then switching to NYC, and then again switching to Tokyo.

The other thing I can mention is for the arrow going out of the producer/consumer box to pass POCO DTO's only. It adds to the value of your application and also makes isolation testing a lot easier.

Testing off live feeds (or even simulated feeds) is scant because they don't capture all the conditions that need to be tested before an app is deployable.

Finally I would mention that the producer/consumer pattern was implemented starting in .NET 4.0 with the System.Collections.Concurrent name space... http://msdn.microsoft.com/en-us/library/dd287147.aspx I have been using these classes in production and they really cut through the need to test a home-grown design pattern.

于 2013-07-03T15:31:08.697 回答