title: How to handle busy indicator in WPF when using async methods on re-entrant calls?
body: <p>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.</p>
  
  <p>When using <code>async</code> methods we just have to ensure that we turn <code>IsBusy = "true"</code> before we call the method and <code>IsBusy="false"</code> after the <code>await</code>.</p>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p><strong>Example scenario</strong></p>
  
  <p>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.</p>
  
  <p>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).</p>
  
  <p>On entering the name the following method is called:</p>
  
  <p>Imagine <code>GetStudentResults</code> 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.</p>
  
  <pre><code>private async void SearchName(string name)
  {
      ResultDisplayGrid.IsBusy = true;
      await GetStudentResults();
      ResultDisplayGrid.IsBusy = false;
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/Nlzx9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Nlzx9.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/6Gw6B.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6Gw6B.png" alt="enter image description here"></a></p>