I'm currently building an async enabled version of an existing API and I'm struggling to find any guidance on when it is a good idea to support cancellation. Some Async methods in the BCL do not have an overload which accepts a CancellationToken
and I found this MSDN article which states
it is not essential for all async methods to support cancellation
So, what conditions would make it worth supporting cancellation via a CancellationToken
?
I'm leaning towards the following conditions:
Are those reasonable conditions? there any others?
This is just my opinion, but I'd say that if the async
methods you're calling all support cancellation, then yours should. In a similar vein, if you're creating an await
able API over a naturally-asynchronous operation, then do your best to support cancellation (e.g., via CancellationToken.Register
).
I'd also say that any (synchronous) CPU-bound method that could take a "long time" should observe a cancellation token periodically (CancellationToken.ThrowIfCancellationRequested
). "Long time" is relative, but as a rough guideline I'd say anything over half a second (on older hardware, not our 8-core dev machines ;).
In any other scenario, you're talking about a much less useful form of cancellation - specifically, the cancellation may take an arbitrary amount of time to take effect. E.g., if some async
methods support it but others don't. I'm not sure how useful a cancellation token parameter would be in that case; you might want to put it in, but be sure to document its limitations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With