What is the use of CancellationToken
's IsCancellationRequested
property? Consider below code
static void Main(string[] args)
{
CancellationTokenSource tokenSource = new CancellationTokenSource();
var token = tokenSource.Token;
Console.WriteLine("Press Enter to Start.\nAgain Press enter to finish.");
Console.ReadLine();
Task t = new Task(() =>
{
int i = 0;
while (true)
{
if (token.IsCancellationRequested)
{
Console.WriteLine("Task Cancel requested");
break;
}
Console.WriteLine(i++);
}
}, token);
t.Start();
// wait for input before exiting
Console.ReadLine();
tokenSource.Cancel();
if(t.Status==TaskStatus.Canceled)
Console.WriteLine("Task was cancelled");
else
Console.WriteLine("Task completed");
}
I find that on rare occasions code inside if
block doesn't run. If so what is the use of polling to see if cancellation is requested?
The problem with your code is that you don't wait for the
Task
to finish. So, what can happen is this:Cancel()
.Status
, which returnsRunning
.Task
is still running.Main()
completes, application exits.IsCancellationRequested
would be checked from the background thread. But that never happens, since the application already exited.)To fix that, add
t.Wait()
after you callCancel()
.But that still won't fix your program completely. You need to tell the
Task
that it was canceled. And you do that by throwingOperationCanceledException
that contains theCancellationToken
(the usual way to do that is to callThrowIfCancellationRequested()
).One issue with that is that
Wait()
ing on aTask
that was canceled will throw an exception, so you will have to catch that.