I'm having a slowness problem with a transaction that pings to see if certain IP's are accessible. It turns out that this transaction tests more than 20 different IP's, which makes the total transaction time is 2 minutes. To decrease the total time of this transaction, I tried to implement asynchronous methods to test these IPs simultaneously, but I did not succeed. Here is the code implemented so far.
public async Task<List<Foo>> Testar(List<Foo> lista)
{
Task<Foo>[] teste = new Task<Foo>[lista.Count];
for (int i = 0; i < lista.Count; i++)
{
teste[i] = Verificar(lista[i].IP);
}
for (int i = 0; i < lista.Count; i++)
{
lista[i].Status = await teste[i];
}
return lista;
}
public async Task<Status> Verificar(string ip)
{
int retorno = await TestarIP(ip);
return ((Status)retorno);
}
public async Task<int> TestarIP(string ip)
{
if(new Ping().Send(ip, timeOutPing).Status.Equals(IPStatus.Success))
return 1;
else
return 2;
}
The code runs as soon as the page loads (in onLoad), but even using Async / Await, is having the same runtime as before. So how can I create threads to test these ip's simultaneously?