How to cancel a long operation that can be undone at many points?

We have a lengthy operation consisting of many points at which it can be canceled. If we were to use the cancellation token approach, we would need to make sure that we check the cancellation at every place we want.

This seems suboptimal to us. Here we see two problems:

  • we need to check the cancellation marker in all places where there is the possibility of cancellation, which we feel is cluttered with code, and there is a chance to miss points in the code on which there are other possibilities for cancellation.
  • If we follow the recommendations to avoid global variables, we need to pass the cancellation token as a parameter to many methods, which, in our opinion, are repeated and clutter up the code.

Should we use the global cancellation token to work with 2.? Should we use AppDomainto work with 1. and 2.?

+4
source share
1 answer

we need to make sure that we check the cancellation at every required place.

This is really the right way to handle this.

The global token does not solve the check at each required place and can only potentially help not to skip tokens as parameters for the methods. This limits you - if you use a global token, you take away the opportunity now or in the future to change your routine in order to have several operations in flight with a separate cancellation.

, , - . API - , , , OperationCanceledException .

, , , . , "" (, , Thread.Abort, ).

+5

All Articles