question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Call NotifyOfPropertyChange in a different thread?

See original GitHub issue

Is it possible to call NotifyOfPropertyChange from a different thread?

I have a ViewModel class with a method called LoadData() which calls NotifyOfPropertyChange for all variables in that class.

I also have a class which uses C#s Timer class to run a background method every 1 minute which fetches new data from DB. This method calls the LoadData method in the ViewModel class. But it does not refresh the UI. Should the LoadData method be called only from the UI thread?

Here are my classes:

public class ShellViewModel : Conductor<object>
{
    public List<MyClass> AllData {get; set;}
    
    public void LoadData()
    {
        NotifyOfPropertyChange(() => AllData);
    }
}

My Timer Class

public class RecurringTasks : PropertyChangedBase
{
    //This method runs in a background thread
    private void UpdateData()
    {
        //Code to connect to DB and get data
        _shellViewModel.AllData = //assign data from DB
        _shellViewModel.LoadData();
}

Whenever the Timer runs the UpdateData() method, I see that the data in the “AllData” field ShellViewModel actually gets updated. But the UI is not changing.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
nigel-sampsoncommented, Dec 16, 2019

That would work but could have side effects in other areas of your application.

I’m going to close this issue out as it’s definitely not a framework bug.

0reactions
gowthamnatarajancommented, Dec 16, 2019

Thanks. Making all ViewModels as Singletons worked and there were no other issues in the application. Its because, I always though all ViewModels would be singleton by default and wrote code accordingly.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WPF and NotifyPropertyChanged from a different thread
It looks like moreless I've found the answer for this specific situation. The exception is not thrown by UiModel and it's binding, but...
Read more >
How to trigger [NotifyPropertyChanged] from background ...
In this case the OnPropertyChanged method is called on worker thread because the StartTogglingAsync method has been started on the worker thread by...
Read more >
Category Model and ViewModel
The NotifyOfPropertyChange method cannot look inside it to see if the contents has changed. To solve this, the contents is removed and completely...
Read more >
ObservableProperty attribute - .NET Community Toolkit
The first two are useful whenever you want to run some logic that only needs to reference the new value that the property...
Read more >
Caliburn Micro Part 2: The Basics of Databinding - System.Mike!
PropertyChangedBase gives us access to NotifyOfPropertyChange which tells the view that it should display the new value every time the Message ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found