So in a GUI program I have multiple events that trigger a update of a workspace. This update call is costly so I would want it to not happen very often.
A user might pick something from a dropdown ( triggers a refresh ) and then put a check in a checkbox (also triggers a refresh ). If he does this in a short time interval this might result in the first refresh to be irrelevant.
One way to handle this would be to have a refresh button for the user to press, or a toggle to set whether to automatically refresh. But I wouldn’t want to put this in the hands of the user.
Is there any other strategy to do this that is proven to be effective and error free?
PS. I’m specifically using .Net 4.0 -> C# -> WPF -> Caliburn.Micro but would like to hear of any method that could be reproduced using .net 4
I have a class for that purpose.
At ViewModel’s initialisation:
DeferredUpdater m_Updater = new DeferredUpdater(TimeSpan.FromSeconds(3), //The amount of time between two updates that has to pass for an update in database to be required
TimeSpan.FromSeconds(0.5), //The polling time
OnUpdateRequested); //The callback to use to update in database.
And you’ll have to hook up the propertychanged event of your viewmodel:
this.PropertyChanged += OnPropertyChanged;
void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
{
m_Updater.DeferUpdate();
}
And of course define the callback:
void OnUpdateRequested()
{
//Call to DAL/database.
}
It postpones update by checking every X amount of time if Y amount of time passed since the last call to DeferUpdate, and does all that in a thread (through a Task).
As far as I know (I have tested it a lot, and use it in a production scenario) it is thread safe.
Just make sure to get the latest version (V2) which is an improved solution.
One way to avoid refreshing too often would be to use a dirty_flag + a Timer; you set the Timer interval to something like 200ms or whatever value you consider optimal.
When en event that requires a refresh happens, you just set the dirty_flag to true, and reinitialize the Timer(that way you make sure that Timer.Tick will be raised at “x” milliseconds after the user action). If another action is taken by the user in that interval of time, you reinitialize the timer again, and so on.
Then, on Timer.Tick: you do the refresh only if the dirty_flag = true, set dirty_flag = false.
An optimization that you can do is to disable the Timer after the refresh, and enable it on the next action that requires a refresh.
If your refresh is non-blocking you could perhaps add a cancellation. When a refresh overlaps with another refresh, first cancel the earlier refresh then invoke a new one.
If the refresh is really expensive perhaps you can localize it somewhat more. Does the drop-down effect everything or could you logically target a sub-set of presented state?