等待与不同的结果多任务

编程入门 行业动态 更新时间:2024-10-25 03:26:56
本文介绍了等待与不同的结果多任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有3个任务:

private async Task<Cat> FeedCat() {} private async Task<House> SellHouse() {} private async Task<Tesla> BuyCar() {}

他们都需要运行之前,我的code可以继续,我需要从每个结果也是如此。结果都没有什么共同点相互

They all need to run before my code can continue and I need the results from each as well. None of the results have anything in common with each other

我如何打电话等待了3个任务完成,然后得到的结果?

How do I call and await for the 3 tasks to complete and then get the results?

推荐答案

在使用后 WhenAll ,您可以用出单独拉动的结果等待:

var catTask = FeedCat(); var houseTask = SellHouse(); var carTask = BuyCar(); await Task.WhenAll(catTask, houseTask, carTask); var cat = await catTask; var house = await houseTask; var car = await carTask;

您也可以使用 Task.Result (因为你知道凭这一点,他们都成功完成)。不过,我建议使用伺机,因为它显然是正确的,而结果可能会导致其他情况下的问题。

You can also use Task.Result (since you know by this point they have all completed successfully). However, I recommend using await because it's clearly correct, while Result can cause problems in other scenarios.

更多推荐

等待与不同的结果多任务

本文发布于:2023-11-15 00:35:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1589563.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!