我这里有一个简单的程序
private static void CancellingSingleTask()
{
DateTime whenStarted = DateTime.Now;
Console.WriteLine("[{0}] - Main: Started", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks));
CancellationTokenSource cts = new CancellationTokenSource();
CancellationToken ct = cts.Token;
Task task = Task.Factory.StartNew(() =>
{
int? taskId = Task.CurrentId;
Console.WriteLine("[{0}] - Task - [{1}]: Started", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks), taskId);
Thread.Sleep(2000);
if (ct.IsCancellationRequested)
{
Console.WriteLine("[{0}] - Task - [{1}]: Cancellation Requested", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks), taskId);
throw new OperationCanceledException();
}
Console.WriteLine("[{0}] - Task - [{1}]: No Cancellation Requested", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks), taskId);
}, ct);
Action Print = () =>
{
Console.WriteLine("[{0}] - Main: Task.IsCanceled = [{1}] Task.IsFaulted = [{2}] Task.IsCompleted = [{3}] Task.Status = [{4}]", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks),
task.IsCanceled, task.IsFaulted, task.IsCompleted, task.Status);
};
Console.WriteLine("[{0}] - Main: Started New Task", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks));
Print();
Thread.Sleep(1000);
Console.WriteLine("[{0}] - Main: Cancelling Task", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks));
cts.Cancel();
Thread.Sleep(2000);
Console.WriteLine("[{0}] - Main: After Cancelling Task", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks));
Print();
try
{
Console.WriteLine("[{0}] - Main: Waiting For Task", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks));
task.Wait();
Console.WriteLine("[{0}] - Main: After Waiting For Task", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks));
Print();
}
catch (AggregateException aggregateException)
{
Thread.Sleep(2000);
Console.WriteLine("[{0}] - Main: In Catch Block", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks));
Print();
foreach (var exception in aggregateException.InnerExceptions)
{
Console.WriteLine("[{0}] - Main: Received Exception In Task [{1}]", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks), exception.Message);
}
}
}
示例输出
[00:00:00.0010000] - 主要:开始了
[00:00:00.0040002] - 主要:开始新任务
[00:00:00.0060003] - 主要:IsCanceled = [False] IsFaulted = [False] IsCompleted = [False] Status = [Running]
[00:00:00.0070004] - 任务 - [1]:开始
[00:00:01.0070576] - 主要:取消任务
[00:00:02.0071148] - 任务 - [1]:取消请求
[00:00:03.0111722] - 主要:取消任务后
[00:00:03.0111722] - 主要:IsCanceled = [False] IsFaulted = [True] IsCompleted = [True] Status = [Faulted]
[00:00:03.0111722] - 主要:等待任务
[00:00:05.0112866] - 主要:在Catch Block中
[00:00:05.0112866] - 主要:IsCanceled = [False] IsFaulted = [True] IsCompleted = [True] Status = [Faulted]
[00:00:05.0112866] - 主要:任务中收到异常[操作已取消。]
我从来没有看到Task.IsCanceled设置为true,我犯了错误或遗漏了一些明显的东西。 我已就此问题进行了一些研究/搜索,但未能找到确凿的答案。
注意:有关StackOverFlow的相关问题 Cancellation of a task task IsCanceled is false, while I canceled Task.IsCancelled doesn't work
答案 0 :(得分:8)
我想你应该将CancellationToken
传递给OperationCanceledException
的构造函数。
if (ct.IsCancellationRequested)
{
Console.WriteLine("[{0}] - Task - [{1}]: Cancellation Requested", TimeSpan.FromTicks(DateTime.Now.Ticks - whenStarted.Ticks), taskId);
throw new OperationCanceledException(ct);
}
TPL会检查两个CancellationToken
是否相同,如果是,它会将您的任务标记为Cancelled
,因此TPL会假定您的任务未被取消。
甚至更好地使用ThrowIfCancellationRequested
方法,就像那个
ct.ThrowIfCancellationRequested();