.NET中的TPL新手。试图了解CancellationToken
以及如何通知他们取消正在执行的任务。以下代码仅传输一个被取消的任务,其中相同的令牌传递给两个任务。我的假设是,如果超时发生在第一个任务上并且执行ctx.Cancel()
我需要一点帮助来理解为什么我只看到一个例外,因为两个任务都应该被取消。我缺少什么,以及如何确保取消任务并且不占用内存资源。
static void Main(string[] args)
{
Console.WriteLine("Starting application");
var ctx = new CancellationTokenSource();
var token = ctx.Token;
try
{
var task1 = new Program().Run("task1", token);
var task2 = new Program().Run("task2", token);
if (!task1.Wait(1000))
ctx.Cancel();
task2.Wait();
}
catch (AggregateException ex)
{
Console.WriteLine("Aggregate Exception occurred");
foreach (var e in ex.InnerExceptions)
{
Console.WriteLine(e.Message);
}
}
catch (Exception e)
{
Console.WriteLine($"Main Exception: {e.Message}");
}
finally
{
Console.WriteLine("Finish Application");
ctx.Dispose();
}
}
private async Task Run(string name, CancellationToken token)
{
while(true)
{
if (token.IsCancellationRequested)
{
Console.WriteLine("Task Cancelled");
token.ThrowIfCancellationRequested();
}
Console.WriteLine($"Executing {name} ...");
await Task.Delay(250, token);
}
}
只抛出一个异常,其他任务发生了什么?此外,Console.WriteLine("Task Cancelled")
从未执行过。
输出:
Starting application
Executing task1 ...
Executing task2 ...
Executing task2 ...
Executing task1 ...
Executing task1 ...
Executing task2 ...
Executing task2 ...
Executing task1 ...
Aggregate Exception occurred
A task was canceled.
Finish Application