如题,做一个循环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了。
求助,!!!!!!!
{
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了。
求助,!!!!!!!
myPing.PingCompleted += new PingCompletedEventHandler(_myPing_PingComleted);把这两句写到循环外面去...记得最后要销毁myPing...
{
.....//处理事件
Pint p = sender as Pint;
p.Dispose();//处理完毕释放资源
}
而你使用了 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);
}
{
new Thread(() =>
{
...
}).Start();
}
或
Ping myPing = new Ping();
myPing.PingCompleted += (s, e) =>
{
};
myPing.SendAsync(dicIp.Value, 1000, null);
怎么用,能举个 demo吗?
用MAC帧组ARP包,无敌了
不过还是用ping吧,多学个ARP也要花时间.net的ping不用的确会一直增加内存,我的程序需要频繁ping,用的还是很久以前用VB写的小COM,效果好很多