How do I get a return value from Task.WaitAll() in a console app?

后端 未结 2 1974
温柔的废话
温柔的废话 2020-12-05 16:51

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 m

2条回答
  •  难免孤独
    2020-12-05 17:36

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

    • Task.WaitAll use await Task.WhenAll
    • Task.WaitAny use 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];
    

提交回复
热议问题