I have a WPF MVVM c# application. In one of my views I have to retrieve data from the database after a button is clicked. The call to the database is really long and so I want to enable a spinner while the call is being made and disable it when the call is done.
So in the method handling the button click (using Command, not code behind) I set the boolean variable to enable the spinner then create a Task
that goes fetches the data and set it to an ObservableCollection<T>
like this
private void GetDataButtonCommand() {
this.IsBusy = true;
var tasks = new List<Task>();
tasks.Add(Task.Factory.StartNew(() =>
{
System.Windows.Application.Current.Dispatcher.BeginInvoke(new System.Action(() =>
{
var GotData= DBHelper.GetData();
_observablecollectionname.Clear();
foreach (var item in GotData)
{
_observablecollectionname.Add(item);
}
}));
}));
var finalTask = Task.Factory.ContinueWhenAll(tasks.ToArray(), datacomplete =>
{
this.IsBusy = false;
});
}
On completion of the Task
I would like to disable the spinner.
The problem that I am running into is that I am unable to see the spinner during the call. The UI is still being blocked, even thou I am using a Task
to execute the database query.
What am I doing wrong?