Your code works but the behaviour I'm looking for is kinda different.
It's something like this: say I'm pinging a range of IP's starting with192.168.1.1 in the main thread using a for loop. At first the PingCompletedCallback returns the ping statistics from 192.168.1.1; then the main thread pings 192.168.1.2. At this point the PingCompletedCallback returns the ping statistics from another host, something like 192.168.1.32 or some other IP from the given range instead of the statistics for 192.168.1.2 and so on when the main thread gets to the next host. The point is that PingCompletedCallback pings all the IP's but not in the order that the main thread tells it to. And I figured that WaitOne does just that - it forces PingCompletedCallback to return the ping statistics from the current IP in the main thread. Or am I missing something? Or maybe I need another approach? I need the asynchronous ping so that the UI does not freeze while pinging the hosts.
Thanks for all your posts, guys.
You're basically forcing the SendAsync to act like the normal Send. If you need them all to come back in the specific order, then you'll have to block and use Send. So do it in another thread to avoid freezing the UI thread.
Something like
//in a button press event or something
new Thread (new ThreadStart(PingAll)).Start();
//now to create the PingAll method
private void PingAll()
{
//Do your Ping.Sends one at a time here
}
If you need to interact with the UI thread once the pinging is done, then you might want to look at the tutorial here
http://www.yoda.arachsys.com/csharp/threads/index.shtml
You can jump ahead to the section on Forms if you want, but it might be a bit confusing.
Have fun,
-mwalts