D
D
Dmitry Sinelnikov2017-05-10 11:22:26
WPF
Dmitry Sinelnikov, 2017-05-10 11:22:26

How to get data from DB every 10 seconds (Entity Framework)?

Hello!
I am currently developing a messaging application.
Its essence is this: there is a FireBird database, there is an application that connects to the database through the Entity Framework, the application has a DataGrid in which data is output from the database like this

Model1 db = new Model1();
db.USER.Load();
usersGrid.ItemsSource = db.USER.Local.ToBindingList();

There is adding and deleting data.
Since the application itself can be run many times and different users can make changes, there is a need to constantly update the data in the DataGrid.
I decided to take the data acquisition into a separate thread (correct me if I'm moving in the wrong direction) in which it will happen every 10 seconds.
db.USER.Load();
But this did not give results. The thread is accessing the database (this can be seen in the application log), but the data in the DataGrid is not updated. Tell me if there is a forced data update in WPF? And I will also be very grateful for efficient articles on multi-threaded programming in C # with WPF and Entity Framework. (because the ones I find are limited to simple things like factorial calculation in a separate thread)
ps I understand that working with a database of 100 simultaneously enabled applications that will access it every 10 seconds is not the best idea and a client-server application should be made. But this is not possible for the reason that there is no access to the server itself, there is only access to the Firebird database.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2017-05-11
@DimKing

The line usersGrid.ItemsSource = db.USER.Local.ToBindingList(); says that specifically this current list of records from the table should be published in the DataGrid. When you execute Load() again, you create new lists that have nothing to do with what is now in the DataGrid. So there is no update.
To understand what has changed in the database since the last read, you can use, for example, the time the record was modified. And every 10 (or how many you want) seconds to read out only the changed records.
Just like that, the changed entries in the list already read and added to the grid cannot be updated. You need to find among the existing lines those that have been updated in accordance with the request and change their details. Since you are using a BindingList, in theory, after changing an entry in the list, the DataGrid should update the entry on the screen.
The schema might look something like this:
The DataGrid on the form is bound (Binding) to a once-created list of messages of type BindingList<> or ObservableCollection<>. Messages that are stored in this list must implement the PropertyChanged pattern. The list is initially read at the start of the form. After creating, filling and binding to the DataGrid, you cannot delete / recreate the list. If you need to reread it in its entirety, execute Clear() and add the entries again. Then, according to the timer, you read the changed or added entries and run through your list and find and change them. Handles PropertyChanged on messages, handles list changes, handles DataGrid update. If your reading from the database does not take long, then you just need to use the DispatcherTimer. It runs on the same thread where the DataGrid and the list of messages live. If this does not suit you and you want to play with threads, then you need to use thread-safe collections and / or arrange synchronization of actions. But this is a topic for another question.

S
Smilley, 2017-05-10
@Smilley

It would be necessary to use MVVM, then it will be possible to force the use of PropertyChanged. In this case, everything will be updated normally.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question