private static async Task AsyncPingTask(List<string> ipaddress) { try { Console.WriteLine("Ping Started"); StringBuilder pingStringBuilder = new StringBuilder(); var pingTasks = ipaddress.Select(ip => { using (var ping = new Ping()) { return ping.SendPingAsync(ip); } }).ToList(); Console.WriteLine("Ping Completed"); await Task.WhenAll(pingTasks); foreach (var pingReply in pingTasks) { pingStringBuilder.Append(pingReply.Result.Address); pingStringBuilder.Append("-->"); pingStringBuilder.Append(pingReply.Result.Status); pingStringBuilder.Append("-->"); pingStringBuilder.Append(pingReply.Result.RoundtripTime.ToString()); pingStringBuilder.AppendLine(); } Console.WriteLine(pingStringBuilder.ToString()); pingStringBuilder.Clear(); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); throw; } }
public static void Main() { List<string> address = new List<string>(); Task t = Task.Run(() => { var reader = new StreamReader(File.OpenRead(Environment.CurrentDirectory+@"\address.csv")); while (!reader.EndOfStream) { var lines = reader.ReadLine(); var values = lines.Split(';'); address.Add(values[0]); } }); Console.WriteLine("List COunt is {0}",address.Count); Stopwatch timeSpan=Stopwatch.StartNew(); t.Wait(); AsyncPingTask(address).Wait(); Console.WriteLine(timeSpan.ElapsedMilliseconds); Console.ReadLine(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)