If I call cancellationTokenSource.Cancel within the task associated with the cancellation token, the OperationCancelledException is correctly thrown, however, task.IsCanceled is NOT always updated and set to true, as would be expected.
The problem can be quickly demonstrated with the following nUnit test:
var cancellationTokenSource = new CancellationTokenSource(); Task task = Task.Factory.StartNew(() => { cancellationTokenSource.Cancel(); cancellationTokenSource.Token.ThrowIfCancellationRequested(); }, cancellationTokenSource.Token); try { task.Wait(cancellationTokenSource.Token); } catch (OperationCanceledException) { } if (task.IsCanceled) { Assert.Pass(); } else { Assert.Fail(); } When I run this test, the test passes, however, when I DEBUG this test (using the Resharper test runner), the test fails.
I don't think this has anything to do with Resharper, I think Resharper just may be creating some conditions that perhaps expose an issue in .Net. Or, maybe I am just doing something completely wrong... Any insights?
Cancelis being called, for example?cancellationTokenSource.IsCancellationRequestedis set totrue, buttask.IsCanceledis still set tofalse. It is boggling my mind.task.Status?StartNew, and only used it in the body.Console.WriteLine("task.Status: {0}", task.Status);right before the if-block, the test passes. But if I remove that call, the test fails.