title: BusyIndicator allows to fire RelayCommand twice
body: <p>I have a WPF application, which follows MVVM.  </p>
  
  <p>To make UI responsive, I'm using TPL for executing long-running commands and <a href="http://wpftoolkit.codeplex.com/wikipage?title=BusyIndicator&amp;referringTitle=Home" rel="nofollow">BusyIndicator</a> to display to user, that application is busy now.  </p>
  
  <p>In one of the view models I have this command:</p>
  
  <pre><code>public ICommand RefreshOrdersCommand { get; private set; }
  
  public OrdersEditorVM()
  {
      this.Orders = new ObservableCollection&lt;OrderVM&gt;();
      this.RefreshOrdersCommand = new RelayCommand(HandleRefreshOrders);
  
      HandleRefreshOrders();
  }
  
  private void HandleRefreshOrders()
  {
      var task = Task.Factory.StartNew(() =&gt; RefreshOrders());
      task.ContinueWith(t =&gt; RefreshOrdersCompleted(t.Result), 
          CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, TaskScheduler.FromCurrentSynchronizationContext());
      task.ContinueWith(t =&gt; this.LogAggregateException(t.Exception, Resources.OrdersEditorVM_OrdersLoading, Resources.OrdersEditorVM_OrdersLoadingFaulted),
          CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.FromCurrentSynchronizationContext());
  }
  
  private Order[] RefreshOrders()
  {
      IsBusy = true;
      System.Diagnostics.Debug.WriteLine("Start refresh.");
      try
      {
          var orders = // building a query with Entity Framework DB Context API
  
          return orders
              .ToArray();
      }
      finally
      {
          IsBusy = false;
          System.Diagnostics.Debug.WriteLine("Stop refresh.");
      }
  }
  
  private void RefreshOrdersCompleted(Order[] orders)
  {
      Orders.RelpaceContent(orders.Select(o =&gt; new OrderVM(this, o)));
  
      if (Orders.Count &gt; 0)
      {
          Orders[0].IsSelected = true;
      }
  }
  </code></pre>
  
  <p><code>IsBusy</code> property is bound with <code>BusyIndicator.IsBusy</code>, and <code>RefreshOrdersCommand</code> property is bound with a button on a toolbar, which is placed inside <code>BusyIndicator</code> in the view of this view model.</p>
  
  <p><strong>The problem</strong>.  </p>
  
  <p>If the user clicks on button not frequently, everything works fine: <code>BusyIndicator</code> hides toolbar with the button, data becomes loaded, <code>BusyIndicator</code> disappears. In the output window I can see pairs of lines:</p>
  
  <blockquote>
    <p>Start refresh. 
    Stop refresh.</p>
  </blockquote>
  
  <p>But if the user clicks on button <em>very</em> frequently, look like <code>BusyIndicator</code> doesn't hide toolbar in time, and two background threads attempt to execute <code>RefreshOrders</code> method, which causes exceptions (and it is OK, because EF <code>DbContext</code> isn't thread-safe). In the output window I see this picture:</p>
  
  <blockquote>
    <p>Start refresh.
    Start refresh.</p>
  </blockquote>
  
  <p>What am I doing wrong?</p>
  
  <p>I've looked into <code>BusyIndicator</code>'s code. I have no idea, what can be wrong there: setting <code>IsBusy</code> just makes two calls to <code>VisualStateManager.GoToState</code>, which, in turn, just makes visible a rectangle, which hides <code>BusyIndicator</code>'s content:</p>
  
  <pre><code>                    &lt;VisualState x:Name="Visible"&gt;
                         &lt;Storyboard&gt;
                            &lt;ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.001" Storyboard.TargetName="busycontent" Storyboard.TargetProperty="(UIElement.Visibility)"&gt;
                               &lt;DiscreteObjectKeyFrame KeyTime="00:00:00"&gt;
                                  &lt;DiscreteObjectKeyFrame.Value&gt;
                                     &lt;Visibility&gt;Visible&lt;/Visibility&gt;
                                  &lt;/DiscreteObjectKeyFrame.Value&gt;
                               &lt;/DiscreteObjectKeyFrame&gt;
                            &lt;/ObjectAnimationUsingKeyFrames&gt;
                            &lt;ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.001" Storyboard.TargetName="overlay" Storyboard.TargetProperty="(UIElement.Visibility)"&gt;
                               &lt;DiscreteObjectKeyFrame KeyTime="00:00:00"&gt;
                                  &lt;DiscreteObjectKeyFrame.Value&gt;
                                     &lt;Visibility&gt;Visible&lt;/Visibility&gt;
                                  &lt;/DiscreteObjectKeyFrame.Value&gt;
                               &lt;/DiscreteObjectKeyFrame&gt;
                            &lt;/ObjectAnimationUsingKeyFrames&gt;
                         &lt;/Storyboard&gt;
                      &lt;/VisualState&gt;
  </code></pre>
  
  <p>...and disables content:</p>
  
  <pre><code>                    &lt;VisualState x:Name="Busy"&gt;
                         &lt;Storyboard&gt;
                            &lt;ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Duration="00:00:00.001" Storyboard.TargetName="content" Storyboard.TargetProperty="(Control.IsEnabled)"&gt;
                               &lt;DiscreteObjectKeyFrame KeyTime="00:00:00"&gt;
                                  &lt;DiscreteObjectKeyFrame.Value&gt;
                                     &lt;sys:Boolean&gt;False&lt;/sys:Boolean&gt;
                                  &lt;/DiscreteObjectKeyFrame.Value&gt;
                               &lt;/DiscreteObjectKeyFrame&gt;
                            &lt;/ObjectAnimationUsingKeyFrames&gt;
                         &lt;/Storyboard&gt;
                      &lt;/VisualState&gt;
  </code></pre>
  
  <p>Any ideas?</p>
  
  <p><strong>Update</strong>.
  The question isn't about how to prevent command from reentrance. I'm wondering about mechanism, which allows to press button twice.</p>
  
  <p>Here's how it works (from my point):</p>
  
  <ul>
  <li><code>ViewModel.IsBusy</code> is bound with <code>BusyIndicator.IsBusy</code>. The binding is synchronous.</li>
  <li>Setter of <code>BusyIndicator.IsBusy</code> calls <code>VisualStateManager.GoToState</code> twice. One of these calls makes visible a rectangle, which overlaps <code>BusyIndicator</code>'s content (the button, in my case).</li>
  <li>So, as I understand, I can't physically achieve the button after I've set <code>ViewModel.IsBusy</code>, because all of these things happen on the same (UI) thread.</li>
  </ul>
  
  <p>But how the button becomes pressed twice??</p>