Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF dispatcher performance (100-200 updates/sec)

In a WPF Window, I've got a line chart that plots real-time data (Quinn-Curtis RealTime chart for WPF). In short, for each new value, I call a SetCurrentValue(x, y) method, and then the UpdateDraw() method to update the chart.

The data comes in via a TCP connection in another thread. Every new value that comes in causes an DataReceived event, and its handler should plot the value to the chart and then update it. Logically, I can't call UpdateDraw() directly, since my chart is in the UI thread which is not the same thread as where the data comes in.

So I call Dispatcher.Invoke( new Action (UpdateDraw()) ) - and this works fine, well, as long as I update max. 30 times/sec. When updating more often, the Dispatcher can't keep up and the chart updated slower than the data comes in. I tested this using a single-thread situation with simulated data and without the Dispatcher there are no problems.

So, my conclusion is that the Dispatcher is too slow for this situation. I actually need to update 100-200 times/sec!

Is there a way to put a turbo on the Dispatcher, or are there other ways to solve this? Any suggestions are welcome.

like image 423
KBoek Avatar asked Nov 08 '10 13:11

KBoek


2 Answers

An option would be to use a shared queue to communicate the data.

Where the data comes on, you push the data to the end of the queue:

lock (sharedQueue)
{
    sharedQueue.Enqueue(data);
}

On the UI thread, you find a way to read this data, e.g. using a timer:

var incomingData = new List<DataObject>();

lock (sharedQueue)
{
    while (sharedQueue.Count > 0)
        incomingData.Add(sharedQueue.Dequeue());
}

// Use the data in the incomingData list to plot.

The idea here is that you're not communicating that data is coming in. Because you have a constant stream of data, I suspect that's not a problem. I'm not saying that the exact implementation as give above is the rest, but this is about the general idea.

I'm not sure how you should check for new data, because I do not have enough insight into the details of the application; but this may be a start for you.

like image 122
Pieter van Ginkel Avatar answered Oct 16 '22 13:10

Pieter van Ginkel


Youre requierments are bonkers- You seriously do NOT need 100-200 updates per second, especialyl as teh screen runs at 60 updates per second normally. People wont see them anyway.

  • Enter new data into a queue.
  • Trigger a pull event on / for the dispatcher.
  • Santize data in the queue (thro out doubles, last valid wins) and put them in.l

30 updates per second are enough - people wont see a difference. I had performacne issues on some financial data under high load with a T&S until I did that - now the graph looks better.

Keep Dispatcher moves as few as you can.

like image 44
TomTom Avatar answered Oct 16 '22 15:10

TomTom