entity-frameworkmvvmdatabase-concurrency

WPF : Update View Model when DB Changes (Concurrency Issue)


I'm currently working on WPF app that is architectured as follows:

So my app does not do any write actions on the DB what-so-ever (Zéro), it only reads data and displays it on the UI.

How can I make my app to be quickly responsive to DB changes, I read about the following solutions but I'm confused and don't know what to use:

As I said, db changes every millisecond (financial data from other sources),

How can resolve this?

Thank you.


Solution

  • i tried the code bellow and it seemed to be working okay for the moment (but i still have some doubts about that infinit loop), let me know what you thing :

    public class MyViewModel
        {
    
    
              BackgroundWorker _bgWorker ;
              //some props
              //some funcs
    
    
    protected internal MyViewModel(Session session)
            {
                Session = session;
                RefreshData();
            }        
    
    protected void RefreshData()
            {
                try
                {
                    _bgWorker = new BackgroundWorker
                    {
                        WorkerReportsProgress = true,
                        WorkerSupportsCancellation = true
    
                    };
                    _bgWorker.DoWork += bgDoWork;
    
                    if (!_bgWorker.IsBusy)
                    {
                        _bgWorker.RunWorkerAsync();
                    }
                }
                catch (Exception)
                {
                    _bgWorker.CancelAsync();
                }
            }
    
            private void bgDoWork(object sender, DoWorkEventArgs e)
            {
                var worker = (BackgroundWorker)sender;
                while (!worker.CancellationPending)
                {
                    //Thread.Sleep(1000); should i keep this or not ?
                    Proxy(); // WCF calls
                }
            }
            }