Combining multiple events into one action/ Defer refreshing

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật