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.