I'm doing a doing a crawler style method that takes a string list, makes a call to a website, inserts the return into another list, and returns.
My method is working, but it is very slow so I want to parallelize the calls.
I searched for Parallel.ForEach
easily solve this problem, but is it safe to use it with List
?
My current code:
private List<Veiculo> ObtemTodosVeiculos(List<string> modelos)
{
List<Veiculo> veiculos = new List<Veiculo>();
foreach (var modelo in modelos)
{
string retorno = GET(string.Format("https://192.168.0.1/modelo/{0}", modelo));
var veiculo = Deserializa<Veiculo>(retorno);
if (veiculo != null)
veiculos.Add(veiculo);
}
return veiculos;
}