Reputation: 11
I use this code for running tasks synchronously
var result = new StringBuilder();
//cts is a CancellationTokenSource();
foreach (var test in Tests)
{
var t1 = new Task<string>(t => test.Run(), cts.Token);
t1.Strat();
result.Append(t1.Result);
}
I call cts.Cancel()
from a different thread and i can see that the token is updated:
cts.Token.IsCancellationRequested == true
But the task keep running and even the next one starts...
what am i missing?
thanks!
Upvotes: 1
Views: 440
Reputation: 13887
From the documentation for CancellationToken.IsCancellationRequested
:
If this property is true, it only guarantees that cancellation has been requested. It does not guarantee that every registered handler has finished executing, nor that cancellation requests have finished propagating to all registered handlers. Additional synchronization may be required, particularly in situations where related objects are being canceled concurrently.
Upvotes: 2