Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to ping a list of IPs but it's not working.

I already use this function in console app and it was working.

C#
string[] addresses = { "192.168.1.2", "192.168.1.3", "192.168.1.4" };
    void show()
    {
        List<Task<PingReply>> pingTasks = new List<Task<PingReply>>();
        foreach (var address in addresses)
        {
            pingTasks.Add(PingAsync(address));
        }

        Task.WaitAll(pingTasks.ToArray());
        int i = 0;
        listView1.Clear();
        listView1.Columns.Add("Device IP", 100);
        listView1.Columns.Add("ping", 60);
        foreach (var pingTask in pingTasks)
        {
            string[] A = { addresses[i], pingTask.Result.RoundtripTime.ToString() };
            ListViewItem item = new ListViewItem(A);
            listView1.Items.Add(item);
            i++;
        }
    }

    Task<PingReply> PingAsync(string address)
    {
        var tcs = new TaskCompletionSource<PingReply>();
        Ping ping = new Ping();
        ping.PingCompleted += (obj, sender) =>
        {
            tcs.SetResult(sender.Reply);
        };
        ping.SendAsync(address, new object());
        return tcs.Task;
    }


please help me.

thank you

What I have tried:

there is not any error in compilation. but when I run it, PC stops working
Posted
Updated 1-Jul-16 9:15am
v2
Comments
Richard MacCutchan 1-Jul-16 4:53am    
Use your debugger to trace the point at which it stops working.

1 solution

You should learn to use the debugger as soon as possible.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Quote:
I want to ping a list of IPs but it's not working.
Is it possible that the PC just wait undefinitely for the end of all threads ? The debugger will will you.
 
Share this answer
 
v2

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