我需要做的就是在for操作里面,每次循环ping不同IP,速度很慢,如何提高?麻烦贴出code,谢谢啦。

解决方案 »

  1.   

    按理说用线程池给每次ping分配个线程,应该能快点
    楼主贴出代码看看
      

  2.   

    你这个,ping 耗的时间不是真正用于CPU 。而是,发送---等待--->接收 这个过程.
    你那个主线程 睡觉去了,等对方回应时才叫醒。   所以,你可以多叫几个人来等,不就好了!---(多线程)
      

  3.   

    new Thread(new ThreadStart(()=>{ PING HERE }).Start();
      

  4.   


    我是想ping个网段所有IP
                for (int i = 0; i < 255; i++) 
                { 
                    //当前需要Ping的IP
                    string strIP = strGateWay + i.ToString();                if (IsPingIP(strIP))
                    { 
                        //添加至在线主机集合
                        arrOnline.Add(strIP);
                    }
                }
      

  5.   

     private static object obj_lock = new object();
            static void Main(string[] args)
            {
                string[] ips = new string[] { "192.168.1.1", "192.168.1.2", "192.168.1.3", "192.168.1.4", "192.168.1.5" };
                int i = -1;
                while (++i < ips.Length)
                {
                    ThreadPool.QueueUserWorkItem(_ =>
                    {
                        lock (obj_lock)
                        {
                            Ping p = new Ping();
                            PingReply pr = p.Send(IPAddress.Parse(ips[(int)_]), 5000);
                            Console.WriteLine(pr.Address + ":" + pr.Status);
                        }
                    }, i);
                }
                Console.ReadKey();
            }
      

  6.   

    Console.WriteLine(pr.Address + ":" + pr.Status);
    改为
    Console.WriteLine(ips[(int)_] + ":" + pr.Status);获取请求地址的回应。
      

  7.   


    int[] sResult  = new int[255];//声成全局变量
    for (int i = 0; i < 255; i++)
    {
    ParameterizedThreadStart pts = new ParameterizedThreadStart(threadPing);
    Thread thisThread = new Thread(pts);
    string strIP = strGateWay + i.ToString();

    //当前需要Ping的IP
    object[] sendPara = new object[2];
    sendPara[0] = strIP;
    sendPara[1]= i; thisThread.Start(strIP);

    }
    public void threadPing(object ip)
    {
    int index = (int)((object[])ip)[1];
    string strIp = (string)((object[])ip)[0];;
    if (IsPingIP(strIP))
    {
    sResult[index]=true;
    }
    else
    {
    sResult[index]=false;
    }
    }
      

  8.   

    int count = 0;
    for (int i = 0; i < 255; i++)
    {
        //当前需要Ping的IP
        string strIP = strGateWay + i.ToString();    new Thread(p =>
        {
            if (IsPingIP((string)p))
            {
                lock (arrOnline)
                {
                    arrOnline.Add((string)p);
                }
            }
            Interlocked.Increment(ref count);
        }).Start(strIP);
    }while (count != 255)
    {
        Thread.Sleep(100);
    }
      

  9.   

    刚才我也试了下ThreadPool,发现了个问题:ThreadPool太抠门了!
    500个空着的,就给我分了9个
      

  10.   

    人家这是合理利用,线程操作是很耗资源的,在同一时间一个CPU只能运行一个线程的。。你自己创建500个线程然后让490多个线程在那阻塞?
      

  11.   


    threadPing函数内无法强制类型噢。
    int index = (int)((object[])ip)[1];这一句。
      

  12.   

    那改一改吧,我用bool 的,声成int 了
    int[] sResult  = new int[255];//声成全局变量
    for (int i = 0; i < 255; i++)
    {
        sResult[i]=0;//数据初始化
    }
    for (int i = 0; i < 255; i++)
    {
        ParameterizedThreadStart pts = new ParameterizedThreadStart(threadPing);
        Thread thisThread = new Thread(pts);
        string strIP = strGateWay + i.ToString();
        
        //当前需要Ping的IP
        object[] sendPara = new object[2];
        sendPara[0] = strIP;
        sendPara[1]= i;    thisThread.Start(strIP);
        
    }
    public void threadPing(object ip)
    {
        int index = (int)((object[])ip)[1];
        string strIp = (string)((object[])ip)[0];;
        if (IsPingIP(strIP))
        {
            sResult[index]=1;
        }
        else
        {
            sResult[index]=-1;
        }
    }// 结果,
    //数组中的 数据为-1 :表示失败;
    //数组中的 数据为 0 :表示线程还在睡觉中,--ping 还没有返回;
    //数组中的 数据为 1 :表示拼成功;
    //
    //这个,如果 sResult 中还有0 ,那你可以等一下,即用一个线程sleep一下,等到数组中没有0为止!
      

  13.   

    代码同上:
    int[] sResult = new int[255];//声成全局变量
    for (int i = 0; i < 255; i++)
    {
      sResult[i]=0;//数据初始化
    }
    for (int i = 0; i < 255; i++)
    {
      ParameterizedThreadStart pts = new ParameterizedThreadStart(threadPing);
      Thread thisThread = new Thread(pts);
      string strIP = strGateWay + i.ToString();
        
      //当前需要Ping的IP
      object[] sendPara = new object[2];
      sendPara[0] = strIP;
      sendPara[1]= i;  thisThread.Start(strIP);
        
    }
    public void threadPing(object ip)
    {
      int index = (int)((object[])ip)[1];
      string strIp = (string)((object[])ip)[0];;
      if (IsPingIP(strIP))
      {
      sResult[index]=1;
      }
      else
      {
      sResult[index]=-1;
      }
    }// 结果,
    //数组中的 数据为-1 :表示失败;
    //数组中的 数据为 0 :表示线程还在睡觉中,--ping 还没有返回;
    //数组中的 数据为 1 :表示拼成功;
    //
    //这个,如果 sResult 中还有0 ,那你可以等一下,即用一个线程sleep一下,等到数组中没有0为止!
      

  14.   

    等下周三VS11 Beta出来了,你可以试下C# 4.5的异步语法。for (int i = 0; i < n; i++)
    {
        int result = await pingip(ip[i]);
    }
      

  15.   


    这代码您测试过了吗?我测试不行,全部变成成功了。thisThread.Start的是sendPara 不是strIP
      

  16.   

    .NET 4.0 的 Parallel.For/Foreach
      

  17.   

    管for循环屌事,如果换成这样,你试一下,会不会更慢for (int i=0;i<10;i++)
    {
    Thread.Sleep(一万年);
    }
      

  18.   

    我的意思是:慢不是for的原因,时间是费在for里的代码处理上,你要在代码逻辑上突破
      

  19.   


    嗯,的确ping的效率不好。你有什么方法去更快实现这个。