更新时间:2022-06-01 09:15:55
您可以使用 TaskExtensions.Unwrap()
(这是 Task< Task>
的扩展方法)来展开外部任务并检索内部任务:
You can use TaskExtensions.Unwrap()
(which is an extension method on Task<Task>
) to unwrap the outter task and retrieve the inner one:
private async void Button2_Click(object sender, RoutedEventArgs e)
{
var task = Task.FromResult(false);
Task aggregatedTask = task.ContinueWith(task1 => InnerTask(task1.Result)).Unwrap();
Console.WriteLine("1");
await aggregatedTask;
Console.WriteLine("4");
}
请注意,为了简化整件事,而不是 ContinueWith
样式延续,您可以等待
完成任务:
Note that to simplify this entire thing, instead of ContinueWith
style continuation you can await
on your tasks:
private async void Button2_Click(object sender, RoutedEventArgs e)
{
var task = Task.FromResult(false);
Console.WriteLine("1");
var result = await task;
await InnerTask(result);
Console.WriteLine("4");
}