0

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.

Gustav
  • 11
  • 4
  • CPU utilization is not a reliable indicator of the amount of actual work being done. Some important questions: what does `GetDataFor`-do? Is it threadsafe? Is it compute or IO limited? You should probably also do some profiling before even trying to do any optimizations. – JonasH Dec 09 '22 at 10:41
  • Starting 1000s of Tasks in parallel might actually be detremental to performance. But it's hard to tell without more context. – Fildor Dec 09 '22 at 10:42
  • 2
    Also, you should not be using `Task.Result`, you should be `await`ing the call to `Task.WhenAny` – DavidG Dec 09 '22 at 10:42
  • 2
    ^^ Generally "go async **all the way**" ... or don't _at all_. – Fildor Dec 09 '22 at 10:51
  • And looking at `ProgressBar.Value++;` you might want to consider `IProgress`. – Fildor Dec 09 '22 at 10:52
  • And in the long run, considering TPL - DataFlow may be something for you. – Fildor Dec 09 '22 at 10:54
  • 1
    You also have introduced a bug by using the loop variabnle `i` in a lambda expression. See here: https://stackoverflow.com/questions/271440/captured-variable-in-a-loop-in-c-sharp – Klaus Gütter Dec 09 '22 at 11:16

0 Answers0