So in a GUI program I have multiple events that trigger a update of a workspace.
ID: 643351 • Letter: S
Question
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
Explanation / Answer
I have a class for that purpose and I blogged about it here.
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.