I am facing a challenge with my project. I need to invoke an API several times within a loop and collect the responses into a List. After gathering the responses, I plan to execute some operations on that List. Currently, I am calling the API sequentially, which means each call waits for the response before proceeding to the next. My goal is to perform these API calls using threads, so I can collect all the responses at once and then apply my business logic on the results.
Here is a sample of my code:
foreach (var item in collection)
{
// intending to initiate a thread here
resultList.Add(MakeApiCall(parameters));
}
// need to ensure all threads complete before continuing.
ApplyBusinessRules(resultList);