Are there any benefits for using a CancellationTokenSource over a volatile boolean field for signalling a Task
to finish?
Of course yes. There are many. I'll list few.
CancellationToken
supports callbacks. You can be notified when the cancellation is requested.CancellationToken
supports WaitHandle
which you could wait for indefinitely or with a timeout.CancellationToken
using CancellationTokenSource.CancelAfter
method.CancellationToken
to another, so that when one is cancelled another can be considered as cancelled.Task
if you mean System.Threading.Tasks.Task
a volatile boolean cannot transition the state of the Task to cancelled but CancellationToken
can.