日期:2014-05-17  浏览次数:21061 次

C#异步多线程ping

   private void button3_Click(object sender, EventArgs e)
        {
           
                //IPAddress address = IPAddress.Parse(Convert.ToString(dataGridView1.Rows[i].Cells[1].Value));
                Thread thread = new Thread(new ThreadStart(_pingThread));
                thread.IsBackground = true;
                thread.Start();
           
        }


        private void _pingThread()
        {

            Ping ping = new Ping();
            ping.PingCompleted += new PingCompletedEventHandler(_myPing_PingCompleted);            

            for(int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (Convert.ToString(dataGridView1.Rows[i].Cells[1].Value) != "")
                {
                    string _ipAddress = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value);
                    //每一行的datagridview中的第二格都有IP地址
                    PingReply result = ping.Send(_ipAddress);

                     if (result.Status != IPStatus.Success)
                      {
                          dataGridView1.Rows[i].Cells[3].Value = result.RoundtripTime.ToString();   
                       }
                }
            }


            
        }


求指导,我现在这个是多线程ping了吧,为啥我这个ping多个地址很慢的,,,别人做出的程序ping多个地址很快。。而我的没效率啊。。求改进。。。