I created a piece of code that needs to receive data through an API request by running a lot of tasks in parallel.
for (decimal i = StartNum.Value; i <= EndNum.Value; i++)
{
foreach (String zip in zips)
{
TList.Add(Task.Run(() => GetDataFor(zip, i)));
}
while (TList.Count > 0)
{
Task result = Task.WhenAny(TList).Result;
TList.Remove(result);
ProgressBar.Value++;
}
}
This code causes a couple thousands of tasks to be created and waits for them all to finish to then again make a new tasklist until EndNum.Value
is reached.
While debugging the code seems to use 100% of the CPU, shown in task manager and the VS diagnostics. However when I build the app and run the exe file outside VS cpu utilisation reaches only 10%. Does this mean there is still a 10 times performance increase to be reached? If so how can I execute my funtion more efficiently so I can reach full utilisation? Note that the actual performance of the app is identical in both cases
I tried using the Parallel.ForEach way but performance seems to be slower this way.