From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

Measurement Studio for .NET Languages

cancel
Showing results for 
Search instead for 
Did you mean: 

After time when data added the UI stuck - WPF

Solved!
Go to solution

I am currently using in WPF Graph.

 

I created a simulation that describes the problem that I have experienced in my application (attached).

 

I have two threads, one generates data (in my application gets data from hardware every second), and the other copies the data to the UI (copies the data to a variable, which bind to the DataSource).

 

Every second I get 1000 points and adds them to the data. The first few seconds it works well, after a while it gets stuck.

 

I added a listbox that displays the time it takes to add the data to the variable and is increases gradually.

 

Capture.PNG

 

I have two questions:
1) Does whenever I add data, it draws all existing data again? if yes, theres a way to improve or prevent this behavior?
2) How much points the graph can represent in a single view? (what is the limitation of the graph?)

 

thanks, 

   Hodaya Shalom.

0 Kudos
Message 1 of 6
(4,661 Views)

Looking at your example, I see you are using a WPF ObservableCollection<Point> to hold your data. The observable collection is not very efficient and will raise an event for every individual Add operation (very similar to the Wpf graph can't auto refresh question).


To answer your first question, the graph does draw all the data it is assigned. The simplest change to improve performance would be to switch from the general-purpose observable object collection to a data-focused collection. Changing the SignalData.Data property in your example to ChartCollection<System.Windows.Point> (with a large capacity), and changing the DisplayValuesThread.SetSignalData implementation to use a single Append( data ) call, greatly improved the example's performance.


The answer to your second question greatly depends on the scenario and how the graph is configured. The graph is capable of handling many millions of samples, and in combination with the chart collection is able to process new data incrementally, also increasing performance. You may also be interested in the Optimizing the WPF Graphs help topic.

~ Paul H
0 Kudos
Message 2 of 6
(4,650 Views)

Actually ObservableCollection<Point> is in use just in my example, in my appliaction I use RangeObservableCollection<Point> that have a AddRange function that raise event after range added (and not per point).

 

I examined the ChartCollection vs the RangeObservableCollection, and the performence is similar.

 

I changed my example to be more like my appliaction (it's added below).

 

and I see that the UI stuck when I get to 1,000,000 points, although the data in cs continue to increase.

 

 

0 Kudos
Message 3 of 6
(4,599 Views)
Solution
Accepted by hodaya273

Debugging your updated example, I believe the issue is the Dispatcher.Invoke calls you are using to communicate with the UI thread. Since you are using the delegate-only overload, all of the calls are sent using Normal priority, which means that lower-priority events (like graph rendering, or data binding updates to scales) will not get processed. Using a lower priority like Render on the four Invoke calls un-stuck the UI in my testing.

~ Paul H
0 Kudos
Message 4 of 6
(4,578 Views)

When I want to display the data in running way and not every second,I divid the data to 5 and draw every 200 msec.

 

After some time I get delay that get bigger and the UI stuck. (project attach)

 

When I draw all data once I dont have a problem and it's run over the milion, and here it stuck before.

 

Is there a problem to draw every 200 msec?

0 Kudos
Message 5 of 6
(4,445 Views)

I can't really offer general guidance about your example, but here are some areas I noticed that you might want to look into further:

 

  • The "Optimizing the WPF Graphs" topic I linked to earlier is a little out of date (it appears we did not update our web help for the latest release). I would suggest looking at the installed documentation on your machine. In particular, Hardware is the new recommended render mode setting for graphs.

  • You are populating the graph with Point scatter values. Even though the points might happen to contain waveform-like data, the graph cannot perform any waveform optimizations on the data. Using a ChartCollection<double,double> would allow the graph to perform more optimized rendering. (I think you mentioned the example is not representative of the actual data in your application, so this may not be relevant.)

  • For scatter data, some of our fallback processing is sub-optimal when dealing with the larger collections of Point data you are using. You should experiment with disabling culling or decimation, or both (the best approach will depend on your actual data), using the technique from strange plots on graphs.

  • Your use of low-level blocking synchronization primitives seems very brittle. The references on producer/consumer patterns in How to keep c# .NET GUI responsive during long operations may be useful to you.
~ Paul H
0 Kudos
Message 6 of 6
(4,418 Views)