469,327 Members | 1,219 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

C# and Ping.SendAsync

Hi everybody.
I'm trying to develop e network scanner. I've run into trouble when got to the part of scanning the IP's :) ... meaning that I need to make the scanning method non-blocking. To do so I thought the Ping.SendAsync would come in handy. I've followed this example: http://msdn2.microsoft.com/en-us/library/system.net.networkinformation.ping.aspx but when I try to use it in a Winforms application the WaitOne() method blocks the PingCompletedCallback method that handles the pinging. I would really appreciate some help.
Jun 6 '07 #1
6 17549
kenobewan
4,871 Expert 4TB
What error are you receiving?
Jun 6 '07 #2
Plater
7,872 Expert 4TB
Your link points to .net3.0 (be sure you have .net3.0 or follow over to link relative to your own version of .net)

Why are you using the WaitOne() call? It's a blocking call. Seems like that would defeat the purpose of your async call?
Jun 6 '07 #3
Plater
7,872 Expert 4TB
I just did this, it will have run conditions, but works,
Expand|Select|Wrap|Line Numbers
  1. Ping pi = new Ping();
  2. private string allofit = "";
  3. byte[] myb = new byte[] { 0x41, 0x42, 0x43, 0x41, 0x42, 0x43 };
  4.  
  5. void Form1_Load(object sender, EventArgs e)
  6. {
  7.    pi.PingCompleted += new PingCompletedEventHandler(pi_PingCompleted);
  8.    int i = 1;
  9.    pi.SendAsync("192.168.1."+i.ToString(), 100, myb, i);
  10. }
  11. void pi_PingCompleted(object sender, PingCompletedEventArgs e)
  12. {
  13.    PingReply pr = e.Reply;
  14.    allofit += e.Reply.Address.ToString() + " " + e.Reply.Status.ToString() + "\r\n";
  15.    int i = (int)e.UserState;
  16.    i=i+1;
  17.    if (i < 10)
  18.    {
  19.       pi.SendAsync("192.168.1." + i.ToString(), 100, myb, i);
  20.    }
  21.    else
  22.    {
  23.       MessageBox.Show(allofit);
  24.    }
  25.    //throw new Exception("The method or operation is not implemented.");
  26. }
  27.  
and a few secs later a window popping up telling me:
192.168.1 {success?]
192.168.2 {success?]
192.168.3 {success?]
192.168.4 {success?]
192.168.5 {success?]
192.168.6 {success?]
192.168.7 {success?]
192.168.8 {success?]
192.168.9 {success?]
Jun 6 '07 #4
I just did this, it will have run conditions, but works,
Expand|Select|Wrap|Line Numbers
  1. Ping pi = new Ping();
  2. private string allofit = "";
  3. byte[] myb = new byte[] { 0x41, 0x42, 0x43, 0x41, 0x42, 0x43 };
  4.  
  5. void Form1_Load(object sender, EventArgs e)
  6. {
  7.    pi.PingCompleted += new PingCompletedEventHandler(pi_PingCompleted);
  8.    int i = 1;
  9.    pi.SendAsync("192.168.1."+i.ToString(), 100, myb, i);
  10. }
  11. void pi_PingCompleted(object sender, PingCompletedEventArgs e)
  12. {
  13.    PingReply pr = e.Reply;
  14.    allofit += e.Reply.Address.ToString() + " " + e.Reply.Status.ToString() + "\r\n";
  15.    int i = (int)e.UserState;
  16.    i=i+1;
  17.    if (i < 10)
  18.    {
  19.       pi.SendAsync("192.168.1." + i.ToString(), 100, myb, i);
  20.    }
  21.    else
  22.    {
  23.       MessageBox.Show(allofit);
  24.    }
  25.    //throw new Exception("The method or operation is not implemented.");
  26. }
  27.  
and a few secs later a window popping up telling me:
192.168.1 {success?]
192.168.2 {success?]
192.168.3 {success?]
192.168.4 {success?]
192.168.5 {success?]
192.168.6 {success?]
192.168.7 {success?]
192.168.8 {success?]
192.168.9 {success?]
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.
Jun 6 '07 #5
mwalts
38
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
Jun 6 '07 #6
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
Thank you very much, mwalts. Your link proved to be very useful. Thank you again.
Jun 8 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Jason Rodman | last post: by
reply views Thread by scotty | last post: by
21 posts views Thread by Neel | last post: by
1 post views Thread by Krish | last post: by
reply views Thread by Sid Price | last post: by
6 posts views Thread by Dave Marden | last post: by
2 posts views Thread by Olivier MATROT | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.