WPF实时多线程股票交易应用程序 [英] WPF Real Time Multithreaded Stock Trading Application

查看:262
本文介绍了WPF实时多线程股票交易应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在WPF中构建一个实时多线程应用程序,但是在更新UI时遇到了困难.

I am building a real-time multi-threaded application in WPF, but i am having difficulties in updating the UI.

我有一个后台工作线程,该线程包含确定哪些交易要发送到市场的逻辑.当有效交易发送到市场时,我会通过我的主应用程序窗口中的事件接收这些交易的状态更新.我还有其他活动可以接收实时价格更新.

I have a background worker thread that contains logic which determines what trades to send into the market. When a valid trade is sent to the market, i receive status updates on these trades via events in my main application window. I have other events where i receive real-time price updates.

通过这些事件,我更新了UI.现在看来,我在整个应用程序中接收事件的速度如此之快,以至于UI无法跟上接收事件的速度-导致UI缓慢更新或根本不更新.从本质上讲,UI冻结了.触发所有事件后,UI会再次缓慢响应.完全响应后,UI会显示我期望的数据.

Through these events, i upate the UI. Now it appears that i receive events so rapidly through out the application, that the UI can't keep up with the speed at which events are received - causing the UI to update slowly or not at all. Essentially the UI freezes. After all events have fired, the UI slowly becomes responsive again. Once it is fully responsive, the UI shows the data that i am expecting.

我的问题是,如何获得与接收事件一样快的UI实时更新?我已经为此苦苦挣扎了一段时间,因此我们将不胜感激.

My question is, how do i get the UI to update in real-time as fast as i receive events? I have been struggling with this for a while now, so any help would be appreciated.

提前谢谢!

推荐答案

与其让辅助线程通过事件将更新推送到UI线程,不如考虑让UI线程定期拉(或轮询)更新.在很多情况下,push方法都可以,但是有两个不利于您的主要缺点.

Instead of having the worker thread push the updates to the UI thread via events consider having the UI thread pull (or poll) them periodically. The push method is fine in a lot of situations but has two major disadvantages that are working against you.

  • 某处有一个昂贵的封送处理操作,该操作正在转移方法的执行以安全地执行UI更新(至少应该是 ).
  • 工作线程可以决定UI应该多久更新一次,并暗示它应该执行多少工作.它很容易使消息泵不堪重负.

我建议使用一个共享队列,其中工作线程将使包含更新的数据结构入队,而UI线程将使该队列出队并对其进行处理.您可以让UI线程以策略性选择的时间间隔轮询队列,以免陷入困境.队列将充当缓冲区,而不是UI消息泵.随着更新的潮起潮落,它会缩小和增长.这是我在说什么的简单图表.

I propose using a shared queue in which the worker thread will enqueue a data structure containing the update and the UI thread will dequeue and process it. You can have the UI thread poll the queue at a strategically chosen interval so that it never gets bogged down. The queue will act as the buffer instead of the UI message pump. It will shrink and grow as the amount of updates ebb and flow. Here is a simple diagram of what I am talking about.

[Worker-Thread] -> [Queue] -> [UI-Thread]

我将首先从简单队列方法开始,但是您可以将其带到创建管道的下一个逻辑步骤,该管道中有3个线程参与更新流程.辅助线程使更新入队,而UI线程则像以前那样使它们出队.但是,可以将新线程添加到混合中,以管理队列中等待的更新数量并将其保持在可管理的大小.如果队列很小,它将通过转发所有更新来做到这一点,但是它将切换到安全模式,并开始丢弃您可以生存的更新,如果可以定义合理的合并操作,则无需合并或将许多合并为一个.这是此模式如何工作的简单示意图.

I would start with the simple queue approach first, but you could take this to the next logical step of creating a pipeline in which there are 3 threads participating in the flow of updates. The worker thread enqueues updates and the UI thread dequeues them like before. But, a new thread could be added to the mix that manages the number of updates waiting in the queue and keeps it at a manageable size. It will do this by forwarding on all updates if the queue remains small, but will switch into safe mode and start discarding the updates you can live without or combining many into one if a reasonable merge operation can be defined. Here is a simple diagram of how this pattern might work.

[Worker-Thread] -> [Queue-1] -> [Pipeline-Thread] -> [Queue-2] -> [UI-Thread]

同样,从简单的一个队列方法开始.如果您需要更多控制权,请转到管道模式.我都成功使用了.

Again, start with the simple one queue approach. If you need more control then move to the pipeline pattern. I have used both successfully.

这篇关于WPF实时多线程股票交易应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆