Follow these steps to update the existing project and to have cancellation support during the long running process:
- Navigate to the MainWindow.xaml file and modify the UI to have a Cancel button in it. Add the following button control inside StackPanel, and label it as Cancel:
<Button x:Name="cancelButton" Content="Cancel" IsEnabled="False" Margin="4" Padding="8 2" Click="OnCancelClicked"/>
- Make sure to set its IsEnabled property to False.
- Now, navigate to the MainWindow.xaml.cs file and add the following member variable inside the class:
private CancellationTokenSource tokenSource = null;
- On the Cancel button click, we need to cancel the running operation. Let's modify the OnCancelClicked event to perform the same as the following code snippet:
private void OnCancelClicked(object sender, RoutedEventArgs e) { if...