This is a web scraping project in C# and responses are the https responses we get but my project works so slow, so I was thinking to change those foreach
calls to parallel.foreach
.
var index = 0;
foreach (var response in responses)
{
if (index == 0)
{
ParseHtmlVatan(response);
}
if (index == 1)
{
ParseHtmlTrendyol(response);
}
if (index == 2)
{
ParseHtmln11(response);
}
index++;
}
2
Answers
You can accomplish this easily using
Parallel.For
(it’s a little less code than usingParallel.ForEach
given your current code).Note that hard-coding the parsing method based on the index is pretty fragile. You might want to store some information in each
response
instance indicating which parser to use.Parallel.ForEach
provides the index as an argument to the lambda.Since it looks like you’ve got a hard-coded set of responses to start with, you could use
Parallel.Invoke
: