Get Results from Task.WhenAll for multiple tasks

john noble 161 Reputation points
2024-10-03T10:19:58.7366667+00:00

Hi Folks,

I am trying to get the results back from running multiple tasks. Each task point to a function and returns either true or false.

Here is my code:

var t1 = new Task(() => { CheckNC(); }) ;

var t2 = new Task(() => { CheckNL(); });

var t3 = new Task(() => { CheckNP(); });

var t4 = new Task(() => { CheckNK(); });

t1.Start();

t2.Start();

t3.Start();

t4.Start();

await Task.WhenAll(t1, t2, t3, t4);

There is no t1.Result property to look up ??

Am I doing this correct or is there a better way?

Visual Studio 2019 on WinForms

Thanks,

J

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,765 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,046 questions
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 67,096 Reputation points
    2024-10-03T15:56:42.7366667+00:00

    Updated by Jiale:

    This is the code with added Progress Controls. It works fine.

    var t1 = Task.Run(() => CheckNC(progress, statusNC));
    
    var t2 = Task.Run(() => CheckNL(progress, statusNL));
    
    var t3 = Task.Run(() => CheckAD(progress, statusAD, token));
    
    var t4 = Task.Run(() => CheckAC(progress, statusAC, token));
    
    // Wait till all tasks are complete
    
    await Task.WhenAll(t1, t2, t3, t4);
    

    your Tasks all return void, so there is no result, just that they completed. only Task<> has a result.

    Task<> example:

    var t1 = new Task<int>(() => 10);
    var t2 = new Task<string>(() => "hi");
    t1.Start();
    t2.Start();
    await Task.WhenAll(t1, t2);
    Console.WriteLine(t1.Result);
    Console.WriteLine(t2.Result);
    }
    

    Task<> example with common result type:

    var t1 = new Task<int>(() => 10);
    var t2 = new Task<int>(() => 20);
    t1.Start();
    t2.Start();
    var list = await Task.WhenAll(t1, t2);
    foreach (var i in list)
    	Console.WriteLine(i);
    

    or simpler syntax:

    var list = await Task.WhenAll(
       Task.Run(() => 10),
       Task.Run(() => 20)
    );
    foreach (var i in list)
    	Console.WriteLine(i);
    
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Michael Taylor 55,136 Reputation points
    2024-10-03T16:19:57.2733333+00:00

    You should not be creating tasks directly. That is the wrong approach. Do something like this.

    var t1 = Task.Run(CheckNC);
    var t2 = Task.Run(CheckNL);
    var t3 = Task.Run(CheckNP);
    var t4 = Task.Run(CheckNK);
    
    await Task.WhenAll(t1, t2, t3, t4);
    
    //Get results
    var result = t1.Result;
    

    Task.Run accepts either a function with no parameters or a function that returns a result. If a function returns a result then the returned task is typed ask Task<T> so the result is available from Result.

    Note that if you want to use WhenAll you can but it is only necessary if you want to wait for all tasks to complete before you process any results. Result and Wait on each task will automatically block, although they will also throw if the underlying task fails.

    //Alternatives to WhenAll
    await Task.WhenAll(t1, t2, t3, t4);
    
    await t1;
    await t2;
    ...
    
    //Or - block on result
    var result1 = t1.Result;
    var result2 = t2.Result;
    ...
    
    //Or - just wait
    t1.Wait();
    t2.Wait();
    ...
    
    1 person found this answer helpful.
    0 comments No comments

  2. john noble 161 Reputation points
    2024-10-04T08:58:13.0266667+00:00

    Thanks guys for your help.

    This is my new code with added Progress Controls. It works fine.

    var t1 = Task.Run(() => CheckNC(progress, statusNC));

    var t2 = Task.Run(() => CheckNL(progress, statusNL));

    var t3 = Task.Run(() => CheckAD(progress, statusAD, token));

    var t4 = Task.Run(() => CheckAC(progress, statusAC, token));

    // Wait till all tasks are complete

    await Task.WhenAll(t1, t2, t3, t4);


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.