如题,做一个循环Ping局域网内网段的IP(注意是循环Timer):代码如下 foreach (KeyValuePair<string, string> dicIp in clientsIp)
 {
    Ping myPing = new Ping()
       myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingComleted);//这里面绝对没问题(因为是空事件)
       myPing.SendAsync(dicIp.Value, 1000, null);
 }发现内存一直在增加
如果代码换成这样: foreach (KeyValuePair<string, string> dicIp in clientsIp)
 {
    using (Ping myPing = new Ping())
    {
       myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingComleted);//这里面绝对没问题(因为是空事件)
       myPing.SendAsync(dicIp.Value, 1000, null);
    }
 }_myPing_PingComleted事件里面的sender,以及e访问会运行时会出错,原因是已经为null了。
求助,!!!!!!!

解决方案 »

  1.   

     用Timer循环后,抓狂啊,内存一直在增加啊!!!!如果加了释放ping的代码,事件又会出错啊!!!!
      

  2.   

    Ping myPing = new Ping()
    myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingComleted);把这两句写到循环外面去...记得最后要销毁myPing...
      

  3.   

    Timer 重入问题,你的timer 间隔最少要设为255*1000ms,否则内存释放的速度赶不上增加速度
      

  4.   

    呵呵,你连续Ping了多少?狂干蛮干肯定要趴窝的。收集地址用arp。
      

  5.   

    void _myPing_PingComleted(Object sernder, EventArges e)
    {
    .....//处理事件
    Pint p = sender as Pint;
    p.Dispose();//处理完毕释放资源
    }
      

  6.   

    myPing.SendAsync 是异步方法
    而你使用了 using ,所以还没开始 ping 呢 Ping 就已经释放了你要这么写foreach (KeyValuePair<string, string> dicIp in clientsIp)
    {
        Ping myPing = new Ping();
        myPing.PingCompleted += (sender, e) =>
        {
            // 处理...
            myPing.Dispose();
        };
        myPing.SendAsync(dicIp.Value, 1000, null);
    }
      

  7.   

    foreach (KeyValuePair<string, string> dicIp in clientsIp)
    {
      new Thread(() =>
      {
      ...
      }).Start();
    }

    Ping myPing = new Ping();
                myPing.PingCompleted += (s, e) =>
                    {
                        
                    };
                myPing.SendAsync(dicIp.Value, 1000, null);
      

  8.   


    怎么用,能举个 demo吗?
      

  9.   

    winpcap  
    用MAC帧组ARP包,无敌了
    不过还是用ping吧,多学个ARP也要花时间.net的ping不用的确会一直增加内存,我的程序需要频繁ping,用的还是很久以前用VB写的小COM,效果好很多
      

  10.   

    看似很简单的ip地址,可是要遍历一个网段里所有可能的ip地址,还多线程,得有多少线程在那里“忙活”啊?!别是,大多数Ping的线程都“发呆”在哪里直到超时。关于arp可以搜索:http://www.google.com.hk/search?hl=zh-CN&safe=strict&q=.net%E5%8F%91%E9%80%81arp%E8%8E%B7%E5%8F%96%E7%BD%91%E6%AE%B5%E5%86%85ip&aq=f&aqi=&aql=&oq=