I have somewhat of a complicated foreach loop. I have to throttle on every 100 connections due to the web server throwing a 529, and I know that in a parallel my throttling would mess up.
Is there a way to make this run in parallel, and still work the same? I've only just started dabbling in Parallel programming and don't think I know a way around this one.
foreach (var userId in IdCollection)
{
i++;
if (i % 100 == 0 && i != 0)
{
Thread.Sleep(15 * 1000);
}
ProcessId(userId, file);
}
ProcessId:
static async void ProcessId(long userId, string file)
{
var username = await Utilities.GetUsernameFromId(userId);
FileUtilities.WriteUsernameToFile(username, file);
IdItemsProcessed++;
}
Method GetUsernameFromId is the one that actually hangs, my reasoning for running in parallel is to hopefully speed this up, I have over 1 million ID's to process and at this rate, it'll take me forever.
It runs a web client to a server, grabbing the response and parsing it into a username. Pretty simple stuff but when you're doing it in big numbers can become a problem.