Discovering patterns for thread cancellation
There are different methods of listening for cancellation requests from a thread or task. So far, we have seen examples of managing these requests by either handling the OperationCanceledException
type or checking the value of IsCancellationRequested
. The pattern of checking IsCancellationRequested
, usually inside a loop, is called polling. First, we will see another example of this pattern. The second pattern we will examine is receiving the notification by registering a callback method. The final pattern that we will cover in this section is listening to cancellation requests with wait handles using ManualResetEvent
or ManualResetEventSlim
.
Let’s start by trying another example of handling a cancellation request by polling.
Canceling with polling
In this section, we will create another example that uses polling to cancel a background task. The previous example of polling was running in a background thread on the ThreadPool...