WPF high frequency data binding

I have a database with a table that records data coming from different sources. 3 columns

'Source' 'Value' 'Timestamp' '

eg.

source1 21 '11: 03 '

source6 22 '11: 03 '

source9 456 '11: 03 '

The table is updated 2 to 3 times per second. Each value may or may not change. Each Source value is displayed in a separate label or text field on the screen (rather than in the grid). I need to find a better method to get this data associated with WPF content. What type of object should store data in a dictionary, DataTable, etc.

What type of object will store the data? How to bind your label or text box to a value.

I request sources 2 to 4 times per second. In the vast majority of cases, the values ​​in the database table will not change. Often only one value changes. Sometimes they all change. I expect that you will have up to 30 unique data sources.

Put your thought caps, please.

+4
source share
4 answers

It’s just by chance that I deal with very similar problems. Like me, I assume that you are updating data in the background thread.

Collections

Use MTObservableCollection instead of ObservableCollection to store data. ObservableCollection is usually used to bind data to materials that can be changed but cannot be updated from the background stream. MTObservableCollection works for me, but does not inherit thread safety, so be careful.

http://www.julmar.com/blog/mark/2009/04/01/AddingToAnObservableCollectionFromABackgroundThread.aspx

Models

Each object must implement the INotifyPropertyChanged interface. This will force them to maintain data binding. In addition, WPF allows you to update properties this way in bound threads.

http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifypropertychanged.aspx

Preference to update through "Clear" and "Add"

Make sure you always update objects whenever you can. Do not just clear the collection and reload it, it will be very expensive and will cause adverse user interface effects. Of course, you can make separate additions and deletions when necessary.

+2
source

I do not quite understand, but this table is updated 2 to 4 times per second. Is it correct?

If so, then setting up a thread to query the table and update the collection of objects in memory that fire the event when the update is updated seems to be a reasonable approach. If you created these INotifyPropertyChanged objects, you could directly bind to the properties of the objects, and then, when they are updated in the background, the user interface will be updated automatically (of course, carefully monitor the sorting of threads).

+1
source

Option 2 - Message Queues

Instead of updating collections in the background thread, just create your deltas (change list). Direct these changes to your GUI thread using Dispatcher.BeginInvoke with a low priority level, possibly Background or ContextIdle.

As soon as the dispatcher picks up your changes, he will launch them in your GUI. This avoids all the unpleasant effects of threading. And by adjusting the priority level, you can configure it to update speed and user responsiveness.

Again, make sure you use updates instead of removing and replacing.

0
source

I would be interested to know if Reactive Extensions can help you in this situation. Using them, instead of β€œpulling” the data into your form, you can β€œpush” the data from the event source to update the controls. Just a thought. Hope this is helpful or at least interesting!

0
source

Source: https://habr.com/ru/post/1299500/


All Articles