System.Net.NetworkInformation.Ping崩溃 [英] System.Net.NetworkInformation.Ping crashing

查看:398
本文介绍了System.Net.NetworkInformation.Ping崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个拥有600多个设备的站点.我正在尝试使用标准的.NET ping类一个个地对它们全部执行ping操作.由于某种原因,该线程崩溃-几天后它才停止响应.它所做的只是对网络上的设备执行ping操作.我们正在使用Microsoft Windows Server 2008 R2. .NET ping类是否有问题?我似乎也遇到了内存泄漏,我猜这是由于ping而引起的.我应该只是编写一个win32 ping dll来为我完成此工作,还是.NET出现问题?

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();
        }
    }
}

推荐答案

在使用Ping的异步版本时,我发现有时Ping永不返回.我可以想象,如果这种情况发生在异步,同步上,它将停滞不前.我为Async找到的答案是设置一个超时时间(比Ping超时时间长),然后在SendAsync重试之前调用SendAsyncCancel.

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.

这篇关于System.Net.NetworkInformation.Ping崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆