title: WPF MVVM await: Cancel task does not stop my progressbar
body: <p>I am working on a .NET 4.5 small WPF/MVVM thing and I am trying to get a progressbar to work correct together with async/await.</p>
  
  <p>I have gotten things to work ok, but I am not able to cancel the task properly. Or, I think the task maybe is cancelled, but the progressbar runs the full length no matter what.</p>
  
  <p>I am calling the "LongRun" method both on "Start" and "Stop", but with the optional cancellation token parameter when I want to stop. This might be an unusual way of doing things, but I think it looks ok - at least in theory :-)</p>
  
  <p>Here are some code:</p>
  
  <p><strong>ViewModel</strong></p>
  
  <pre><code>public MainViewModel()
      {
          _progress = new Progress&lt;int&gt;(ReportProgress);
          CountFilesCreatedCommand = new RelayCommand&lt;IProgress&lt;int&gt;&gt;(progress =&gt; LongRun(Progress));
          _tokenSource = new CancellationTokenSource();
          _tokenSource.Cancel();
          StopCountFilesCommand = new RelayCommand&lt;CancellationToken&gt;(token =&gt; LongRun(Progress, _tokenSource.Token));
          ProgressText = "...";
      }
  
      private void ReportProgress(int result)
      {
          PercentProgress = result;
      }
  
      private async void LongRun(IProgress&lt;int&gt; progress, CancellationToken token = default(CancellationToken))
      {
          try
          {
              // Start long running task here
              int i = 0;
              for (; i != 101; ++i)
              {
                  if (token.IsCancellationRequested)
                  {
                      // This part of the code is executed when I
                      // try to stop the Task, I can see the "Cancelled!"
                      // text flicker for a second, and then the code
                      // resume to run
                      _tokenSource.Cancel();
                      progress?.Report(PercentProgress);
                      ProgressText = "Cancelled!";
                   }
                  await Task.Delay(TimeSpan.FromSeconds(0.1), token).ConfigureAwait(false);
                                      progress?.Report(i);
                  ProgressText = i + "%";
              }
          }
          catch (OperationCanceledException)
          {
  
              //throw;
          }
      }
  </code></pre>
  
  <p><strong>XAML</strong></p>
  
  <pre><code>&lt;Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"&gt;
      &lt;StackPanel&gt;
          &lt;!--&lt;Button Margin="10,18,10,0" Content="Start Long Run" Height="30" Click="Button_Click_Async"/&gt;--&gt;
          &lt;ProgressBar x:Name="pbStatus" Margin="10,10,10,0" Height="30" IsIndeterminate="False" Visibility="Visible" Minimum="0" Maximum="100" Value ="{Binding PercentProgress, Mode=TwoWay}"/&gt;
          &lt;TextBlock x:Name="tbResultat" Margin="0,10,0,10" HorizontalAlignment="Center" VerticalAlignment="Center" Text="{Binding ProgressText}" /&gt;
          &lt;DockPanel&gt;
              &lt;Button Margin="10,0" Content="Start Long Run" Height="30" Width="200" Command="{Binding CountFilesCreatedCommand}"/&gt;
              &lt;Button Margin="10,0" Content="Stop Long Run" Height="30" Width="200" HorizontalAlignment="Right" Command="{Binding StopCountFilesCommand}"/&gt;
          &lt;/DockPanel&gt;
      &lt;/StackPanel&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p>I have a feeling that the solution might have something to do with the XAML?</p>
  
  <p>Any help would be greatly appreciated!</p>