功能为:监测局域网机器上线、在线、下线,采用ping的异步调用方法,用timer组件每隔一段时间ping局域网内所有计算机一次,但是每ping一次内存都要增加个3m左右,采用gc.collection()去释放内存也不行,不知是什么原因,请高手指点。代码如下: private void timer1_Tick(object sender, EventArgs e)
{
PingEmployees();
}
public void PingEmployees()
{
try
{
foreach( GHPersonInfo person in m_PersonScanArray ) //GHPersonInfo 为自定义类,
{
Ping myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
string pingIP = person.IpAddress;
int timeout = 256;
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
PingOptions options = new PingOptions(32, true);
myPing.SendAsync(pingIP, timeout, buffer, options);
}
}
catch(Exception exc)
{
MessageBox.Show( "Exception: " + exc.Message);
} } public void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
txtInfo.Text += "Ping canceled.";
} // If an error occurred, display the exception to the user.
if (e.Error != null)
{
txtInfo.Text += "Ping failed:";
txtInfo.Text += e.Error.ToString();
}
PingReply reply = e.Reply;
if (reply == null)
return; if (reply.Status == IPStatus.Success)
{
GetEmployeesStatus(reply.Address.ToString(), IPStatus.Success.ToString()); //如果ping通,那么据当前时间,记录局域网机器的状态
} }
{
PingEmployees();
}
public void PingEmployees()
{
try
{
foreach( GHPersonInfo person in m_PersonScanArray ) //GHPersonInfo 为自定义类,
{
Ping myPing = new Ping();
myPing.PingCompleted += new PingCompletedEventHandler(PingCompletedCallback);
string pingIP = person.IpAddress;
int timeout = 256;
string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte[] buffer = Encoding.ASCII.GetBytes(data);
PingOptions options = new PingOptions(32, true);
myPing.SendAsync(pingIP, timeout, buffer, options);
}
}
catch(Exception exc)
{
MessageBox.Show( "Exception: " + exc.Message);
} } public void PingCompletedCallback(object sender, PingCompletedEventArgs e)
{
// If the operation was canceled, display a message to the user.
if (e.Cancelled)
{
txtInfo.Text += "Ping canceled.";
} // If an error occurred, display the exception to the user.
if (e.Error != null)
{
txtInfo.Text += "Ping failed:";
txtInfo.Text += e.Error.ToString();
}
PingReply reply = e.Reply;
if (reply == null)
return; if (reply.Status == IPStatus.Success)
{
GetEmployeesStatus(reply.Address.ToString(), IPStatus.Success.ToString()); //如果ping通,那么据当前时间,记录局域网机器的状态
} }
这个做了什么事?看到不到
{
myPing.SendAsync(pingIP, timeout, buffer, options);
}
http://www.cnblogs.com/Gildor/archive/2009/09/08/1562225.html?login=1#commentform
找到答案。