How to Perform Multiple “Pings” in Parallel using C#

前端 未结 6 1451
粉色の甜心
粉色の甜心 2020-12-08 11:48

I am trying to calculate the average round-trip time for a collection of servers. In order to speed things up, I would like to perform the pings in parallel. I have writte

6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-08 12:44

    Maybe using SendPingAsync like this:

    using (var ping = new Ping())
    {
        var replies = await Task.WhenAll(hosts.Select(x => ping.SendPingAsync(x)))
                                .ConfigureAwait(false);
                                // false here   ^ unless you want to schedule back to sync context
        ... process replies.
    }
    

提交回复
热议问题