Dowemo
0 0 0 0


Question:

I have a class with properties that are bound to my view. To keep my view up-to-date, I implement INotifyPropertyChanged and raise the event everytime some property changes.

Now I got some heavy functions that freeze my application. I want to put them into a background task.

First: here my current approach

(e.g. on button click)

private async void HeavyFunc()
{
    foreach (var stuff)
    {
        count += await Task.Run(() => stuff.Fetch());
    }
    if (count == 0)
        //...
}

stuff class

public async Task<int> Fetch()
{
    //network stuff
    RaisePropertyChanged("MyProperty");
}
public async void RaisePropertyChanged(string pChangedProperty)
{
    await Application.Current.Dispatcher.BeginInvoke(
        System.Windows.Threading.DispatcherPriority.Normal,
        new ThreadStart(() =>
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(pChangedProperty);
        }
    );
}

The code above gives an exception ("DependencySource" must be created in the same thread like "DependencyObject").

AFAIK, you generally need to create a new thread and run it (while awaiting it). ´await Task.Run(...);´ should do this job.

Since the PropertyChanged event directly influences the UI, calling it in the UI thread seems to be a good decision. This is why I call Dispatcher.BeginInvoke.

What I don't understand: the exception above is caused when different threads are responsible for the data. But I explicitely calling the event on my UI-thread and the object should be created by the UI-thread too. So why do I get an exception?

My main question is: How do I implement the events for the INotifyPropertyChanged interface generally to avoid or handle most of the async programming problems like above? What should be considered while constructing the functions?


Best Answer:


Now I got some heavy functions that freeze my application.

If you're really doing asynchronous "network stuff", then it shouldn't be freezing the app.

My main question is: How do I implement the events for the INotifyPropertyChanged interface generally to avoid or handle most of the async programming problems like above?

The approach that I prefer is to not handle this in the event raising code. Instead, structure the rest of your code so that it respects the UI layer.

In other words, divide your "service" (or "business logic") code from your "UI" code so that it works like this:

// In StuffService class:
public async Task<Result> FetchAsync()
{
  //network stuff
  return result;
}
// In StuffViewModel class:
public async void ButtonClicked()
{
  foreach (var stuff)
  {
    var result = await Task.Run(() => _stuffService.FetchAsync());
    MyProperty = result.MyProperty;
    count += result.Count;
  }
  if (count == 0)
    //...
}
public Property MyProperty
{
  get { return _myProperty; }
  set
  {
    _myProperty = value;
    RaisePropertyChanged();
  }
}
private void RaisePropertyChanged([CallerMemberName] string pChangedProperty = null)
{
  PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(pChangedProperty));
}

This way, there's no manual thread jumping, all properties have the standard ViewModel implementation, the code is simpler and more maintainable, etc.

I did leave in the call to Task.Run, although this should be superfluous if your network calls are truly asynchronous.




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs