如何在控制台应用程序中从Task.WaitAll()获取返回值?

时间:2022-07-22 02:23:14

I am using a console app as a proof of concept and new need to get an async return value.

我使用控制台应用程序作为概念证明和获得异步返回值的新需求。

I figured out that I need to use Task.WaitAll() in my main method to avoid needing an async "main()" method, which is illegal.

我发现我需要在main方法中使用Task.WaitAll()来避免需要异步“main()”方法,这是非法的。

I'm now stuck trying to figure out an overload that allows me to use generics or just returns an object that I can cast, but while in Main().

我现在卡住试图弄清楚允许我使用泛型的重载,或者只返回我可以强制转换的对象,但是在Main()中。

2 个解决方案

#1


42  

You don't get a return value from Task.WaitAll. You only use it to wait for completion of multiple tasks and then get the return value from the tasks themselves.

您没有从Task.WaitAll获得返回值。您只能使用它等待多个任务的完成,然后从任务本身获取返回值。

var task1 = GetAsync(1);
var task2 = GetAsync(2);
Task.WaitAll(task1, task2);
var result1 = task1.Result;
var result2 = task2.Result;

If you only have a single Task, just use the Result property. It will return your value and block the calling thread if the task hasn't finished yet:

如果您只有一个Task,只需使用Result属性。如果任务尚未完成,它将返回您的值并阻止调用线程:

var task = GetAsync(3);
var result = task.Result;

It's generally not a good idea to synchronously wait (block) on an asynchronous task ("sync over async"), but I guess that's fine for a POC.

在异步任务上同步等待(阻塞)通常不是一个好主意(“异步同步”),但我猜这对于POC来说很好。

#2


20  

For best practice, use the new async way of doing things. Instead of

为了获得最佳实践,请使用新的异步处理方式。代替

  • Task.WaitAll use await Task.WhenAll
  • Task.WaitAll使用await Task.WhenAll
  • Task.WaitAny use await Task.WhenAny
  • Task.WaitAny使用await Task.WhenAny

The code above can be written as:

上面的代码可以写成:

var task1 = GetAsync(1);
var task2 = GetAsync(2);
var results = await Task.WhenAll(task1, task2);
var result1 = results[0];
var result2 = results[1];

#1


42  

You don't get a return value from Task.WaitAll. You only use it to wait for completion of multiple tasks and then get the return value from the tasks themselves.

您没有从Task.WaitAll获得返回值。您只能使用它等待多个任务的完成,然后从任务本身获取返回值。

var task1 = GetAsync(1);
var task2 = GetAsync(2);
Task.WaitAll(task1, task2);
var result1 = task1.Result;
var result2 = task2.Result;

If you only have a single Task, just use the Result property. It will return your value and block the calling thread if the task hasn't finished yet:

如果您只有一个Task,只需使用Result属性。如果任务尚未完成,它将返回您的值并阻止调用线程:

var task = GetAsync(3);
var result = task.Result;

It's generally not a good idea to synchronously wait (block) on an asynchronous task ("sync over async"), but I guess that's fine for a POC.

在异步任务上同步等待(阻塞)通常不是一个好主意(“异步同步”),但我猜这对于POC来说很好。

#2


20  

For best practice, use the new async way of doing things. Instead of

为了获得最佳实践,请使用新的异步处理方式。代替

  • Task.WaitAll use await Task.WhenAll
  • Task.WaitAll使用await Task.WhenAll
  • Task.WaitAny use await Task.WhenAny
  • Task.WaitAny使用await Task.WhenAny

The code above can be written as:

上面的代码可以写成:

var task1 = GetAsync(1);
var task2 = GetAsync(2);
var results = await Task.WhenAll(task1, task2);
var result1 = results[0];
var result2 = results[1];