功能为:监测局域网机器上线、在线、下线,采用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通,那么据当前时间,记录局域网机器的状态
            }        }

解决方案 »

  1.   

    GHPersonInfo 为自定义类
    这个做了什么事?看到不到
      

  2.   

    GHPersonInfo类就是定义了一些属性,没有方法,没有别的东西。
      

  3.   

    GHPersonInfo类里定义了姓名、编号、部门、ip地址等属性
      

  4.   

    把那一句   GetEmployeesStatus(reply.Address.ToString(), IPStatus.Success.ToString()); 注释掉看看。
      

  5.   

    如果你发现某种操作不会使“每ping一次内存都要增加个3m左右”了,那么要注意,许多操作应该“copy”PingCompletedEventArgs e返回的数据,而不是直接拿它的返回值传出去。
      

  6.   

    谢谢楼上,我回去试了一下,目前还没有找到最终的答案,但是已经给了我很好的思路了。还想继续问一下,一般是在什么情况下是“copy”PingCompletedEventArgs e返回的数据,而不是直接拿它的返回值传出去?
      

  7.   

    已经查到问题就出在myPing.SendAsync(pingIP, timeout, buffer, options);可是不知道如何去解决,请高人再指点一下,谢谢。public void PingEmployees()
    {     
        myPing.SendAsync(pingIP, timeout, buffer, options);    
    }
      

  8.   

    已经找到答案,是System.Net.NetworkInformation.Ping存在的“内存泄漏”问题 ,在博客园中
    http://www.cnblogs.com/Gildor/archive/2009/09/08/1562225.html?login=1#commentform
    找到答案。