我真的不明白这一点。在我下面的代码中,按原样运行,token.ThrowIfCancellationRequested() 抛出的异常不会被发送回 Main 使用 .Wait() 的位置,而是当场抛出,堆栈跟踪并不知道它发生在哪里除了“任务已取消”。但是,如果我删除 async 关键字并使用 await Task.Delay() 删除 try catch 块,它确实会被发送回 main 中的 .Wait() 并被捕获。
我是不是做错了什么,或者我究竟如何让 await Task.Delay() 和 token.ThrowIfCancellationRequested() 抛出的异常都冒泡到 .Wait()?
static void Main(string[] args)
{
var t = new CancellationTokenSource();
var task = Task.Factory.StartNew((x) => Monitor(t.Token), t.Token, TaskCreationOptions.LongRunning);
while (true)
{
if (Console.ReadLine() == "cancel")
{
t.Cancel();
try
{
task.Wait();
}
catch(AggregateException)
{
Console.WriteLine("Exception via task.Wait()");
}
Console.WriteLine("Cancelled");
}
}
}
static async void Monitor(CancellationToken token)
{
while(true)
{
for(int i = 0; i < 5; i++)
{
// If the async in the method signature, this does not send the exception to .Wait();
token.ThrowIfCancellationRequested();
// Do Work
Thread.Sleep(2000);
}
// Wait 10 seconds before doing work again.
// When this try block is removed, and the async is taken out of the method signature,
// token.ThrowIfCancellationRequested() properly sends the exception to .Wait()
try
{
await Task.Delay(10000, token);
}
catch(TaskCanceledException)
{
Console.WriteLine("Exception from Delay()");
return;
}
}
}