In my Blazor Server app, how can I ping to several IP's at the same time, without blocking each other? In my case the pings are executed one after one

Viewed 14

This is my code where I can ping to one machine without problem. How can I ping to several machines at the same time? I have read several questions but I couldn't adapt the solutions to my case. I am calling the Pingmachine twice , but the two pings are executed one by one not at the same time.

private async Task PingAddress()
{
     if (this.ipAddress != string.Empty)
        await PingMachine(10.92.116.73);
        await PingMachine(10.92.116.74); 
}

private async ValueTask PingMachine(int timesToPing = 4, ipadress)
{
    log.AppendLine($"Pinging {ipAddress}");
    StateHasChanged();
    await Task.Delay(1);
    var counter = 1;
    while (counter <= timesToPing)
    {
        var reply = await Pinger(ipAddress);
        
        log.AppendLine($"Pinged {ipAddress} : {counter} times => address: {reply.Address} - time:{reply.RoundtripTime} - status: {reply.Status.ToString()}");
        counter++;
        StateHasChanged();
    }
}

private async ValueTask<PingReply> Pinger(string ipAddress)
{
    var ping = new Ping();
    var reply = await ping.SendPingAsync(ipAddress);
    return reply;
}
0 Answers
Related