I have a wpf application with all viewModel inheriting from a class NotifyPropertyChangeClass implementing INotifyPropertyChanged (see below).
I want to throttle the notifications sent to the view for avoiding lagging due to too much notification sent. What I achieve so far is to delay for each property using the method Sample of reactive extention.
The problem is that the Gui is refreshed late by the throttlingPeriod. It would feel more responsive to have a first event raised at the starting of the period like this :
The code of NotifyPropertyChangeClass :
using System;
using System.ComponentModel;
using System.Reactive.Linq;
namespace MyNameSpace
{
public class NotifyPropertyChangeClass : INotifyPropertyChanged
{
public NotifyPropertyChangeClass(int throttlingPeriod)
{
var obs = Observable.FromEventPattern<PropertyChangedEventHandler, PropertyChangedEventArgs>(
h => this.privatePropertyChanged += h, h => this.privatePropertyChanged -= h);
var groupedByName = obs.Select(o => o.EventArgs.PropertyName).GroupBy(x => x).SelectMany(o => o.Sample(TimeSpan.FromMilliseconds(throttlingPeriod)));
groupedByName.Subscribe(o =>
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(o));
});
}
public event PropertyChangedEventHandler PropertyChanged;
private event PropertyChangedEventHandler privatePropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = privatePropertyChanged;
if (handler != null)
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
How can I achieve what I want?