Correctly Implementing PingAsync In A Function?

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 933 Views - Last Post: 03 March 2013 - 07:25 PM Rate Topic: -----

#16 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 753
  • Joined: 31-August 11

Re: Correctly Implementing PingAsync In A Function?

Posted 26 February 2013 - 11:38 PM

View PostSkydiver, on 26 February 2013 - 08:04 PM, said:

This seems to work for me without any deadlock:
using System;
using System.Diagnostics;
using System.Data;
using System.Text;
using System.Net;
using System.Threading;
using System.Net.NetworkInformation;

namespace SimpleCsConsole2012
{

public class Program
{
    static void Main()
    {
        IPAddress ip = new IPAddress(0x7F000001);
        byte[] buffer = new byte[8192];
        if (ip != null)
        {
            AutoResetEvent are = new AutoResetEvent(false);
            Ping p = new Ping();

            p.PingCompleted += (o, e) =>
            {
                Console.WriteLine("Ping completed...");
                ((AutoResetEvent)e.UserState).Set();
            };
            p.SendAsync(ip, 7000, buffer, new PingOptions { Ttl = 64, DontFragment = true }, are);
            Console.WriteLine("Waiting...");
            are.WaitOne();
            Console.WriteLine("Done waiting...");
        }
        Console.WriteLine("Done.");
    }
}

}



Quite odd friend and I appreciate your help very much. I do want to use the asynchronous version of ping I have my reasons for later but the weird part about this code is it deadlocks and THE EVENT NEVER EVENT METHOD CODE NEVER GETS FIRED AT ALL!! How crazy is that? I figured that out using breaks etc.? I wonder why that could possibly be or if this is something totally unrelated to my program?
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,531
  • Joined: 05-May 12

Re: Correctly Implementing PingAsync In A Function?

Posted 27 February 2013 - 06:12 AM

You should not get a deadlock. The completed event should be called within timeout milliseconds if the ping fails, or less time if the ping succeeds. Perhaps you are just not waiting long enough.

Try using 127.0.0.1 first to see if you can ping your own computer first. Unless loopback has been disabled, you should get a response. If that is working, then move on to known good IP addresses.
Was This Post Helpful? 1
  • +
  • -

#18 adn258  Icon User is offline

  • D.I.C Addict

Reputation: 11
  • View blog
  • Posts: 753
  • Joined: 31-August 11

Re: Correctly Implementing PingAsync In A Function?

Posted 03 March 2013 - 07:25 PM

View PostSkydiver, on 27 February 2013 - 06:12 AM, said:

You should not get a deadlock. The completed event should be called within timeout milliseconds if the ping fails, or less time if the ping succeeds. Perhaps you are just not waiting long enough.

Try using 127.0.0.1 first to see if you can ping your own computer first. Unless loopback has been disabled, you should get a response. If that is working, then move on to known good IP addresses.


Thanks skydiver it still isn't working. I haven't a clue why but I just used the basic ping in any case which is acceptable as this is way to complicated for something that should be simple which may not even be the fault of .Net or anything but there's something on my end that's causing extreme complication.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2