4

So, I have a site with over 600 devices. I'm trying to ping them all one by one using the standard .NET ping class. For some reason, this thread is crashing - it just stops responding after a few days. All it does is ping devices on the network. We're using Microsoft Windows Server 2008 R2. Is there any problems with the .NET ping class? I also seem to be experiencing memory leaks which I'm guessing is due to the pinging. Should I just write a win32 ping dll to do the job for me or am I doing something wrong with .NET?

private void PingDevice(out bool state, string IP)
{
    PingReply pingReply;
    System.Net.NetworkInformation.Ping pingSender = null;
    state = false;
    try
    {
        pingSender = new System.Net.NetworkInformation.Ping();
        pingReply = pingSender.Send(IP, 4000);
        state = (pingReply.Status == IPStatus.Success); // comms is on/off
    }
    catch (Exception ex)
    {
        PingGlobals.driverThread.LogIt("$E Pinging Devices:" + ex.Message + ", " + IP);
    }
    finally
    {
        if (pingSender != null)
        {
            ((IDisposable)pingSender).Dispose();
        }
    }
}
J. Steen
  • 15,470
  • 15
  • 56
  • 63

3 Answers3

2

In using the asynchronous version of Ping, I have found that sometimes Ping never returns. I could imagine that if this happens on Async, on Sync, it would just stall. The answer I have found for Async is to set up a timeout (longer than the Ping timeout), then call SendAsyncCancel before SendAsync to retry.

PaulL
  • 21
  • 2
1

Creating new instances of the System.Net.NetworkInformation.Ping class seems to leak memory on Server 2008. If you keep one (or a hundred) instances around and reuse them that problem goes away. Just be very careful accessing it from multiple threads.

Eric Nicholson
  • 4,053
  • 1
  • 28
  • 29
1

This is an issue reported for .Net versions below 3.5
There is a Microsoft article describing the issue and the solutions for it here.

noobob
  • 532
  • 4
  • 12