WPF Real Time Multithreaded Stock Trading Application

前端 未结 2 1265
执念已碎
执念已碎 2021-02-02 17:49

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

2条回答
  •  猫巷女王i
    2021-02-02 18:03

    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.

    • There is an expensive marshaling operation somewhere that is transferring execution of a method to perform the UI updates safely (at least there should be).
    • The worker thread gets to dictate how often the UI should update and by implication how much work it should perform. It can easily overwhelm the message pump.

    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]
    

    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.

提交回复
热议问题