using System; using System.Collections.Generic; using System.Net; using System.Net.NetworkInformation; using System.Threading.Tasks; using SiteStatus.Dbo; namespace SiteStatus.Business { public class HostBusiness { public async Task GetHostStatusAsync(string host, int count, int timeout) { var hostStatus = new HostStatusDbo { Ip = host, Times = new List() }; var tasks = new List>(); for (var i = 0; i < count; ++i) { try { var ping = new Ping(); var pingTask = ping.SendPingAsync(host, timeout); tasks.Add(pingTask); } catch (Exception e) { hostStatus.Times.Add(null); } } foreach (var pingResult in await Task.WhenAll(tasks)) { if (pingResult != null && pingResult.Status == IPStatus.Success) { hostStatus.Times.Add(pingResult.RoundtripTime); } else { hostStatus.Times.Add(null); } } try { var dnsResult = await Dns.GetHostEntryAsync(host); if (dnsResult != null) { hostStatus.Hostname = dnsResult.HostName; } else { hostStatus.Hostname = null; } } catch (Exception e) { hostStatus.Hostname = null; } return hostStatus; } } }