I've created two lists of tasks, and I'd like to start processing both lists concurrently.
Although the total number of tasks is relatively small (perhaps less that 10 in total), processing time for each task varies, and I can start two tasks simultaneously as they target a different API.
To differentiate the API I've created two lists, and have populated each task list based on the API that will be utilized.
Within each list of task, I'd like their tasks processed sequentially; however, the processing order in list is not important.
At any given instance, two tasks could be running concurrently as long as the two tasks are not members of same task list.
My first thought was to build along these lines:
var lgoTasks = new List<Task>();
foreach (var x in lgo)
{
var t = sClient.SubmitToMarket(x.Id);
lgoTasks.Add(t);
}
var opApiTasks = new List<Task>();
foreach (var x in opApi)
{
var t = sClient.SubmitToMarket(x.Id);
opApiTasks.Add(t);
}
await Task.WhenAll(lgoTasks, opApiTasks);
But it fails for two (or more?) reason
- WhenAll() won't accept two (or more) Task Arrays
- Even if WhenAll() accepted two Task Arrays, the processing isn't sequential
Next I tried this approach:
var lgoCount = lgo.Count();
var opApiCount = opApi.Count();
var maxCounter = lgoCount > opApiCount ? lgoCount : opApiCount;
try
{
for (int i = 0; i < maxCounter; i++)
{
if (lgoCount - 1 >= i && opApiCount - 1 >= i)
{
var x = sClient.SubmitToMarket(lgo[i].Id);
var y = sClient.SubmitToMarket(opApi[i].Id);
await Task.WhenAll(x, y);
}
else if (lgoCount - 1 >= i)
{
await sClient.SubmitToMarket(Convert.ToInt32(lgo[i].Id));
}
else if (opApiCount - 1 >= i)
{
await sClient.SubmitToMarket(Convert.ToInt32(opApi[i].Id));
}
}
And although it works somewhat, WHENALL() creates a blocker until both its tasks are completed.
How do I concurrently start each list of tasks, making sure just one task from each list is running, and without creating blockers (like I did) using WHENALL()? Before my method returns control to the caller, all tasks in each task list are required to finish.
Thanks kindly for considering my question, and I look forward to your comments.