First, maybe you donβt understand what IsCanceled means? This does not mean that "this Task waiting for cancellation, so it should be completed soon", it means that "this Task been canceled, it is now completed."
If you do not understand this correctly, think about exactly what sequence of events. What's happening:
ThrowIfCancellationRequested() is called, but the token has not yet been canceled, so it does not drop. CalledThread.Sleep() , so the thread starting Task is asleep.Cancel() .IsCanceled . The code in Task not able to understand that the token was canceled, so it still works, so IsCanceled returns false .ThrowIfCancellationRequested() is called again, this time it throws, which actually cancels Task .
This is why IsCanceled returns false you. If you want it to return true , you could add something like Thread.Sleep(150) before checking IsCanceled or, better yet, actually expect Task to complete.
svick source share