Click here to Skip to main content
15,916,692 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I'm trying to read ip address from .csv file and ping those addresses using PingAsync class. But I'm getting above exception in the following line.


Ping Method
C#
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;
            }

            }



Main Method

C#
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();
        }


If I done any mistake please guide me.
Posted
Comments
Sergey Alexandrovich Kryukov 13-Aug-15 3:10am    
Did you read in documentation "Application should check the inner exception"? Please do it.
—SA
Sarath kumar.N 13-Aug-15 3:30am    
Hi! Thanks for your reply! I have done a mistake in a file.Now I changed that. It's working fine!
Sergey Alexandrovich Kryukov 13-Aug-15 3:45am    
Great! And you are very welcome.
Can we close the issue now? I put a formal answer and added a bit to my comment. You may want to accept it formally.
—SA

1 solution

Just to close the issue: please see:
https://msdn.microsoft.com/en-us/library/system.net.networkinformation.pingexception%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.net.networkinformation.ping%28v=vs.110%29.aspx[^].

In the documentation on the exception, pay attention: "Application should check the inner exception". This is a good advice on exception handling in general (and then you have to check them all, recursively), but essential in this case, because it gives your the root-cause exception information.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900