I am using following code to ping to multiple IP's at the same time (one ping repeated 4 times). In general it works fine. But the only problem I have is, when I want to get the ping replies to show them for example on the screen.
Let's say ip1 is online and the ping reply is OK. ip2 is offline and the ping reply is "timed Out" When I want to show the ping results on the screen, while pinging I want that for ip1 the ping result is there immediatly when the reply comes. But the "OK" results for ip1 is coming delayed with the "timed out" replies of ip2.
How can I isolate the ping results so that in my example ip1's result is shown immediatly without waiting each time 4 seconds for the "timed out" message?
private async ValueTask PingMachine()
{
var timesToPing = 4;
var counter = 1;
while (counter <= timesToPing) {
var reply = await Pinger();
foreach (var r in reply) {
TagService.log_PLC.AppendLine($" Pinged {TagService.IP_PLC_List} {counter} times time:{r.RoundtripTime} status: {r.Status.ToString()}");
}
}
counter++;
}
private async Task<IEnumerable<PingReply>> Pinger()
{
List<string> addresses = new List<string>();
if (Check_IP_Correct(TagService.IP_PLC_List) == true)
{
addresses.Add(TagService.IP_PLC_List);
}
var tasks = addresses.Select(async ip => await new Ping().SendPingAsync(ip, 1000));
var reply = await Task.WhenAll(tasks);
return reply;
}