Adding cancelation support to long running threads
When you execute a long running process on a different thread, to keep the UI responsive during the operation, you may want to provide a functionality to cancel the operation. This can be done on an on-demand basis.
In this recipe, we will learn how to add cancellation support to the existing long running operation that we have built in the previous recipe.
Getting ready
We will be using the same example that we used in the previous recipe. You can copy the entire CH10.ThreadingDemo1
project folder and give it a new name, CH10.ThreadingDemo2
. Launch Visual Studio and open the new (CH10.ThreadingDemo2
) project inside it.
How to do it...
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 aCancel
button in it. Add the following button control insideStackPanel
, and label it asCancel
:
<Button x:Name="cancelButton" ...