In WPF controls (e.g. grid), we can usually set a boolean property to show a control is busy loading data and in the UI this will lead to a "Loading..." indicator.
When using async
methods we just have to ensure that we turn IsBusy = "true"
before we call the method and IsBusy="false"
after the await
.
However in case where I can call the grid load method multiple times when the first call completes it will turn the busy indicator off even when the second call is in progress.
Any way to resolve this? I can set a global counter for storing the number of request and set the status of indicator based on the count of this global variable, but its a dirty approach and will not scale if I have multiple asyn events in my code.
Example scenario
In the image below, I can search name of students and for each name my service would get back details (marks, etc.) and display it in the second grid.
I want to show a busy indicator while the second grid is waiting for data (otherwise the user might not know if the program is doing anything).
On entering the name the following method is called:
Imagine GetStudentResults
takes 5 seconds (for every call). I enter first name on 0 second, then at 3 seconds I enter another name. Now at 5 seconds the first call returns and it turns off the busy indicator, while the second name details are not retrieved. This is what I want to avoid.
private async void SearchName(string name)
{
ResultDisplayGrid.IsBusy = true;
await GetStudentResults();
ResultDisplayGrid.IsBusy = false;
}