title: RelayCommand change canExecute automatic
body: <p>The current step of learning MVVM is RelayCommand for me.</p>
  
  <p>So i came up with this RelayCommand class:</p>
  
  <p><strong>Relay Command class</strong></p>
  
  <pre><code>public class RelayCommand : ICommand
  {
      private readonly Action&lt;object&gt; _execute;
      private readonly Func&lt;object, bool&gt; _canExecute;
  
      public RelayCommand(Action&lt;object&gt; execute) : this(execute, null)
      {
  
      }
      public RelayCommand(Action&lt;object&gt; execute, Func&lt;object, bool&gt; canExecute)
      {
          _execute = execute ?? throw new ArgumentNullException(nameof(execute));
          _canExecute = canExecute ?? (x =&gt; true);
      }
  
  
  
      public bool CanExecute(object parameter)
      {
          return _canExecute(parameter);
      }
  
      public void Execute(object parameter)
      {
          _execute(parameter);
      }
  
      public event EventHandler CanExecuteChanged
      {
          add =&gt; CommandManager.RequerySuggested += value;
          remove =&gt; CommandManager.RequerySuggested -= value;
      }
  
      public void Refresh()
      {
          CommandManager.InvalidateRequerySuggested();
      }
  }
  </code></pre>
  
  <p><strong>View Code-Behind</strong></p>
  
  <p>To test if <code>CanExecute</code> is true or false, I created a <code>Click</code> Event which is calling the Command if <code>CanExecute == true</code> or Show an Error Message when <code>CanExecute == false</code>.</p>
  
  <pre><code>if (sender is Button button)
  {
      if (_viewModel.MyCommand.CanExecute(button.Tag)) // Also testet to set this parameter `null`
          _viewModel.MyCommand.Execute(button.Tag);
      else
          ErrorMessage.Error("CanExecute = false");
  }
  </code></pre>
  
  <p><strong>ViewModel</strong></p>
  
  <p>In my ViewModel I created the Command and added a <code>Thread.Sleep()</code> to have time that <code>canExecute</code> can show me the ErrorMessage from the <code>Code-Behind</code>.</p>
  
  <pre><code>public ICommand MyCommand { get; set; }
  public ViewModel()
  {
      MyCommand = new RelayCommand(MyCommandMethod);
  }
  
  public async void MyCommandMethod(object obj)
  {
      await Task.Run(() =&gt;
      {
          Thread.Sleep(5000);
          ErrorMessage.Error(obj as string);
      });
  
  }
  </code></pre>
  
  <p>The Problem now is, that if I click the Button 5 times for example, that <code>MyCommandMetod()</code> is used 5 times. So <code>CanExecute</code> will never change. </p>
  
  <p>But why isn't it changing?</p>
  
  <p>I understand RelayCommand as this:</p>
  
  <ul>
  <li>1st - Button is clicked </li>
  <li>2nd - canExecute = false (wait till process is finished) </li>
  <li>3rd - canExecute = true </li>
  <li>4th - Button can be executed again.</li>
  </ul>
  
  <p>So that u can't spam Button clicks and crash the application if for example someone use SpeedClicker and clicks 1.000.000 times a seconds or so.</p>