expressappframework-405253-ui-construction-views-refresh-data-in-view-after-period-of-time-blazor.md
The following example refreshes data in a List View at a specified time interval:
File: MySolution.Blazor.Server\Controllers\AutoRefreshDataController.cs
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.Blazor;
public class AutoRefreshDataController : ViewController<ListView> {
private readonly System.Timers.Timer timer = new();
public AutoRefreshDataController() {
TargetViewNesting = Nesting.Root;
timer.Interval = 3000;
timer.Elapsed += (s, e) => RefreshData();
}
private void RefreshData() {
((BlazorApplication)Application).InvokeAsync(() => {
bool canRefresh = View is not null && View.SelectedObjects.Count is 0;
if (canRefresh) {
View.ObjectSpace.Refresh();
}
});
}
protected override void OnActivated() {
base.OnActivated();
timer.Start();
}
protected override void OnDeactivated() {
base.OnDeactivated();
timer.Stop();
}
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
timer.Dispose();
}
}
The controller in this example adds the following functionality:
RefreshData event handler is executed.Timer.Elapsed is raised on a thread pool thread, the RefreshData event handler must not execute XAF code directly to avoid competing with another thread for access to shared resources. The BlazorApplication.InvokeAsync method ensures that only one thread executes XAF logic at a time.null, and no objects are currently selected - otherwise the selection could be lost after the data is refreshed.See Also