-1

I have the next code:

if(AObjList.Count > 0)
{
  var ARes = await insertA(AObjList);
  if (!ARes) return false;
}

if(BObjList.Count > 0)
{
  var BRes = await insertB(BObjList);
  if(!BRes) return false;
}

return true;

I would like to run the two async functions(insertA() and insertB) in parallel, but if I do

List<Task> tasks = new List<Task>();
if(AObjList.Count > 0)
{
  tasks.Add(insertA(AObjList));
}

if(BObjList.Count > 0)
{
  tasks.Add(insertB(BObjList));
} 

await Task.WhenAll(tasks);

I can't get the results of the tasks to check them.

Is there a good way to do that?

Thank you!

Edit: the question was marked as duplicated, but for me it's not because in this case you may run the async function or not. So I can't do:

 if(AObjList.Count > 0)
{
  var ATask = insertA(AObjList);
}

if(BObjList.Count > 0)
{
  var BTask = insertB(BObjList);
}

var ARes = await ATask; // ERROR
var BRes = await BTask  // ERROR

return true;

Because the ATask and the BTask variables are inside the "if" condition scope.

Liam
  • 27,717
  • 28
  • 128
  • 190
Kane
  • 375
  • 1
  • 6
  • 18

5 Answers5

1

As return type is Boolean you can do like this , and also attache AsyncState Object with task to identify task

Read here for detail : TPL : Identify and Find Task

List<Task<bool>> tasks = new List<Task<bool>>();
//rest of the code 
if(AObjList.Count > 0)
{
  //"Task A" state object to idetify its task A
  tasks.Add(Task.Factory.StartNew(()=> insertA(AObjList), "Task A"));
}

if(BObjList.Count > 0)
{
  //"Task B" state object to idetify its task B
  tasks.Add(Task.Factory.StartNew(() => insertB(BObjList), "Task B"));
} 
await Task.WhenAll(tasks);
foreach(var t in tasks)
{
  Console.WriteLine(t.AsyncState.ToString());
  Console.WriteLine(t.Result);
}
Pranay Rana
  • 175,020
  • 35
  • 237
  • 263
1

Presuming your Task return bool and you want to return false if any of them return false you can do this thus:

//need to specify the return type here
List<Task<bool>> tasks = new List<Task<bool>>();
if(AObjList.Count > 0)
{
  tasks.Add(insertA(AObjList));
}

if(BObjList.Count > 0)
{
  tasks.Add(insertB(BObjList));
} 

//await the result as usual
await Task.WhenAll(tasks);

//you can only use Result here because you've awaited. Never use this without await 
//as this can cause deadlocks
return tasks.All(a => a.Result);
Liam
  • 27,717
  • 28
  • 128
  • 190
0

You can assign each task to a separate variable and then use them to get value, e.g.:

var taskA = insertA(AObjList);
var taskB = insertB(BObjList);

var tasks = new List<Task>{taskA, taskB};

await Task.WhenAll(tasks);

var resultA = await taskA;
var resultB = await taskB;
Alex Riabov
  • 8,655
  • 5
  • 47
  • 48
-2

Sure you can:

List<Task> tasks = new List<Task>();
if(AObjList.Count > 0)
{
  tasks.Add(insertA(AObjList));
}

if(BObjList.Count > 0)
{
  tasks.Add(insertB(BObjList));
} 

await Task.WhenAll(tasks);

if (tasks.Any(t => !t.Result))
{
  return false;
}
Jirka Veselka
  • 55
  • 1
  • 3
-2

you can use parallel task.

bool aResult;
bool bResult;   

     Parallel.Invoke(() =>
                                     {
                                        if(AObjList.Count > 0)
    {
       aResult = await insertA(AObjList);

    }
                                     },  // close first Action

                                     () =>
                                     {
                                         if(BObjList.Count > 0)
    {
       bResult = await insertB(BObjList);

    }
                                     }
                                 ); //close parallel.invoke

for more detail see https://learn.microsoft.com/en-us/dotnet/standard/parallel-programming/how-to-use-parallel-invoke-to-execute-parallel-operations

Liam
  • 27,717
  • 28
  • 128
  • 190
anoop
  • 91
  • 1
  • 7
  • and where is the result? what is `GetCountForWord`? You seem to of just made this up? – Liam Jun 19 '18 at 10:02
  • @Liam i dont think i have to write the code exactly the same. this code is from MSDN site. this is a sample code . "You seem to of just made this up" . better ask some one from microsoft :) – anoop Jun 19 '18 at 10:07
  • So what your saying is you have cut and pasted this from MSDN? In that case you need to make it as a quote – Liam Jun 19 '18 at 10:10
  • This code doesn't compile... Using you'd need to `async () =>` also doing that is a waste of time. `Parallel.Invoke` is designed for synchronous tasks not async ones. `async` methods already run in parallel. There's no need to use a thread as well. – Liam Jun 19 '18 at 10:13
  • yea. i pasted i copied it from MSDN. sorry. I thought he will get an idea about the parallel code by seeing that code block. – anoop Jun 19 '18 at 10:14
  • There is no benefit in using Parallel.ForEach here as tasks run in parallel by themselves already. That's the whole point of asynchronous code. – ckuri Jun 19 '18 at 10:39
  • @ckuri thanks ckuri. – anoop Jun 19 '18 at 13:59