Adding cancellation and progress with BackgroundWorker

The BackgroundWorker supports two extra features: cancellation and progress. We already looked at cooperative cancellation with the CancellationTokenSource and CancellationToken types, but the BackgroundWorker defines its own mechanism (it was created long before the latter types were).

In this recipe, we'll take a look at adding cancelation to the primes counting endeavor. Also, in a long running operation it's nice to indicate to the user how things are progressing. The BackgroundWorker has good support for that, too.

Getting ready

We'll continue with the CH11.AsyncCalcWithBackgroundWorker project. Alternatively, you can make a copy of it and work on that.

How to do it...

We'll add a Cancel

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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.