Let's pretend I have something like this:
<TextBox Text="{Binding Text, Mode=TwoWay}" />
And something like this:
public class MyViewModel : INotifyPropertyChanged
{
public MyViewModel()
{
// run DoWork() when this.Text changes
Observable.FromEventPattern<PropertyChangedEventArgs>(this, "PropertyChanged")
.Where(x => x.EventArgs.PropertyName.Equals("Text"))
.Subscribe(async x => await DoWork());
}
private async Task DoWork()
{
await Task.Delay(this.Text.Length * 100);
}
public event PropertyChangedEventHandler PropertyChanged;
private string _Text = "Hello World";
public string Text
{
get { return _Text; }
set
{
_Text = value;
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs("Text"));
}
}
}
In this scenario, the user could be typing very quickly. I need:
DoWork() must not run while DoWork() is already running
The user may type in spurts, some changes, pause, some changes
DoWork() is NOT required for every change, only the last change
There is no need for DoWork() to be called more frequently than 1 second
DoWork() cannot wait until the last change, if the spurt is > 1 second
DoWork() should not be called while the system is idle
The duration of DoWork() varies based on the length of this.Text
The question isn't if Rx can do this. I know it can. What's the proper syntax?