A CancellationToken enables cooperative cancellation between threads, thread pool work items, or Task objects. You create a cancellation token by instantiating a CancellationTokenSource object, which manages cancellation tokens retrieved from its CancellationTokenSource.
The CancellationToken is used in asynchronous task. The CancellationTokenSource token is used to signal that the Task should cancel itself. In the above case, the operation will just end when cancellation is requested via Cancel() method.
My question is about safety of checking cancellationToken status in another thread respect of the main thread of the job . Cancellation tokens are generally thread safe by design so passing them between threads and checking them should not be a problem.
Cancellation is a way to signal to an async task that it should stop doing whatever it happens to be doing. In . NET, this is done using a CancellationToken. An instance of a cancellation token is passed to the async task and the async task monitors the token to see if a cancellation has been requested.
You can implement your work method as follows:
private static void Work(CancellationToken cancelToken)
{
while (true)
{
if(cancelToken.IsCancellationRequested)
{
return;
}
Console.Write("345");
}
}
That's it. You always need to handle cancellation by yourself - exit from method when it is appropriate time to exit (so that your work and data is in consistent state)
UPDATE: I prefer not writing while (!cancelToken.IsCancellationRequested)
because often there are few exit points where you can stop executing safely across loop body, and loop usually have some logical condition to exit (iterate over all items in collection etc.). So I believe it's better not to mix that conditions as they have different intention.
Cautionary note about avoiding CancellationToken.ThrowIfCancellationRequested()
:
Comment in question by Eamon Nerbonne:
... replacing
ThrowIfCancellationRequested
with a bunch of checks forIsCancellationRequested
exits gracefully, as this answer says. But that's not just an implementation detail; that affects observable behavior: the task will no longer end in the cancelled state, but inRanToCompletion
. And that can affect not just explicit state checks, but also, more subtly, task chaining with e.g.ContinueWith
, depending on theTaskContinuationOptions
used. I'd say that avoidingThrowIfCancellationRequested
is dangerous advice.
You can create a Task with cancellation token, when you app goto background you can cancel this token.
You can do this in PCL https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/app-lifecycle
var cancelToken = new CancellationTokenSource();
Task.Factory.StartNew(async () => {
await Task.Delay(10000);
// call web API
}, cancelToken.Token);
//this stops the Task:
cancelToken.Cancel(false);
Anther solution is user Timer in Xamarin.Forms, stop timer when app goto background https://xamarinhelp.com/xamarin-forms-timer/
You can use ThrowIfCancellationRequested
without handling the exception!
The use of ThrowIfCancellationRequested
is meant to be used from within a Task
(not a Thread
).
When used within a Task
, you do not have to handle the exception yourself (and get the Unhandled Exception error). It will result in leaving the Task
, and the Task.IsCancelled
property will be True. No exception handling needed.
In your specific case, change the Thread
to a Task
.
Task t = null;
try
{
t = Task.Run(() => Work(cancelSource.Token), cancelSource.Token);
}
if (t.IsCancelled)
{
Console.WriteLine("Canceled!");
}
You have to pass the CancellationToken
to the Task, which will periodically monitors the token to see whether cancellation is requested.
// CancellationTokenSource provides the token and have authority to cancel the token
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
CancellationToken token = cancellationTokenSource.Token;
// Task need to be cancelled with CancellationToken
Task task = Task.Run(async () => {
while(!token.IsCancellationRequested) {
Console.Write("*");
await Task.Delay(1000);
}
}, token);
Console.WriteLine("Press enter to stop the task");
Console.ReadLine();
cancellationTokenSource.Cancel();
In this case, the operation will end when cancellation is requested and the Task
will have a RanToCompletion
state. If you want to be acknowledged that your task has been cancelled, you have to use ThrowIfCancellationRequested
to throw an OperationCanceledException
exception.
Task task = Task.Run(async () =>
{
while (!token.IsCancellationRequested) {
Console.Write("*");
await Task.Delay(1000);
}
token.ThrowIfCancellationRequested();
}, token)
.ContinueWith(t =>
{
t.Exception?.Handle(e => true);
Console.WriteLine("You have canceled the task");
},TaskContinuationOptions.OnlyOnCanceled);
Console.WriteLine("Press enter to stop the task");
Console.ReadLine();
cancellationTokenSource.Cancel();
task.Wait();
Hope this helps to understand better.
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