4
I’m making one by making a style method Crawler which receives a string list, makes a call on a website, inserts the return into another list and returns.
My method is working, but it’s too slow, so I want to parallelize the calls.
So I researched the Parallel.ForEach
easily solves this problem, but it is 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;
}
best solution to your problem is to make your method
GET
asynchronous. This way you would get a Task list and could useTask.WaitAll
to wait for all "at the same time". But be careful with this. Your methodGET
have to use asymcnrone methods as well. It’s not just get there and writeasync
that everything will work wonder... The best is to use a client who supports this– Bruno Costa