Adding cancelation support

Long running operations are typically performed on a different thread, which keeps the UI responsive, but that may not be enough. Applications may want to provide a way to cancel a long-running operation. Prior to .NET 4, developers used various ways to orchestrate cancelations. Starting from .NET 4 there is a standard way to convey cancelation requests.

In this recipe, we'll see how to use this cancelation mechanism.

Getting ready

Open the CH11.AsyncCalc project. We'll enhance it with cancelation support.

How to do it...

We'll add the option to cancel the prime calculation operation.

  1. Open MainWindow.xaml. Add another button to the second StackPanel with the following markup:
    <Button Content="Cancel" Padding="4" Margin="10,0,0,0" ...

Get Windows Presentation Foundation 4.5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.