I have code that sends web requests through two parallel for each loops. Will adding threading before this method occurs cause a delay in the execution of these tasks or will it achieve more web requests?
for (int i = 0; i < threads; i++)
{
populateClient(ServerID, debugLog, type, mC);
tasks[i] = Task.Factory.StartNew(() =>
{
testMaps(ServerID, DebugLog, Type, mC.cl, mC.keywords, mC.locations);
});
}
while (tasks.Any(t => !t.IsCompleted)) { } //spin wait
//…
static void testMaps(Int64 serverID, String debugLog, String type, ClientInfo cl,
List<Keyword> keywords,
List<String> locations)
{
Parallel.ForEach(keywords, keyword =>
{
Parallel.ForEach(locations, location =>
{
strFileName = file.DownloadString(query);
//..
Your program is fine. I/O like this can run in parallel with a relatively high amount of parallelism degree, so launching many tasks, both explicitly as well as through
Parallel.ForEachis not a problem. The implementation will generally manage them well and not cause unwanted overheads, as it is based on the underlying thread pool.