3

我有IP地址的数据表中的大名单,我有这么快ping通他们, 我用这个代码:ping IP地址的大名单的最快方法是什么?

public bool PingIP(string IP) 
{ 
    bool result = false; 
    try 
    {     
     Ping ping = new Ping(); 
     PingReply pingReply = ping.Send(IP); 
     if (pingReply.Status == IPStatus.Success) 
      result = true; 
    } 
    catch 
    { 
     result = false; 
    } 
    return result; 
} 

然后我把它在while循环:

private void CheckNetworkState() 
{ 
    while (rowindexping > -1) 
    { 
     if (rowindexping == tbl_ClientInfo.Rows.Count) 
     { 
      rowindexping = -1; 
      return; 
     } 

     string ip = tbl_ClientInfo.Rows[rowindexping]["clientip"].ToString(); 
     if (!PingIP(ip)) 
     { 
      do something 
     } 
     rowindexping++; 
     Thread.Sleep(100); 
    } 
} 

因为我想在我的项目的背景我调用类的线程来完成这项工作:

threadping = new Thread(CheckNetworkState);    
threadping.IsBackground = true; 
threadping.Start(); 

我的问题是,它需要这么多的时间,并没有在后台工作。我的意思是系统正忙,直到tbl_clientinfo中的所有IP地址都通过ping类。 我想让系统检查所有行,因为我正在处理我的项目的其他部分。

我做得对吗?

+4

平是不会要快。最糟糕的情况是,每个ping数据包都会被忽略/丢失/丢失,您必须等待每次ping测试的超时时间。为了使这个“快速”,你需要同时发射多个ping。 –

+0

ping的速度不是来自代码的百分之百,它直到通信和网络 –

+0

您可以使用ServerToPing []并创建多个并行工作的线程。然后,你可以使用并行库或类似的信号量来运行和规则线程... – Marco

回答

0

我会做的是在后台运行一个单独的多线程守护进程,执行ping操作并将结果放入数据库中。有一个页面稍后通过AJAX加载结果。

3

您的代码正在单线程上运行所有代码;你没有使用多个线程。另外,你为什么在那里有Thread.Sleep

尝试以下操作:

  1. 查询数据库并获得all的IP地址
  2. 在一个循环中,旋转了一个新的线程将运行PingIP每个 IP地址

注意:您也可以在每次从数据库获得新行时启动一个新线程

示例:

static void Main(string[] args) 
    { 
     // get the IPs from the database so you can iterate over them 
     List<string> ips = new List<string>() 
     { 
      "google.com", 
      "127.0.0.1", 
      "stackoverflow.com" 
     }; 

     foreach (var ip in ips) 
     { 
      // See http://stackoverflow.com/questions/4744630/unexpected-behaviour-for-threadpool-queueuserworkitem 
      // for reason to use another variable in the loop 
      string loopIp = ip; 
      WaitCallback func = delegate(object state) 
      { 
       if (PingIP(loopIp)) 
       { 
        Console.WriteLine("Ping Success"); 
       } 
       else 
       { 
        Console.WriteLine("Ping Failed"); 
       } 
      }; 
      ThreadPool.QueueUserWorkItem(func); 
     } 

     Console.ReadLine(); 
    } 

    public static bool PingIP(string IP) 
    { 
     bool result = false; 
     try 
     { 
      Ping ping = new Ping(); 
      PingReply pingReply = ping.Send(IP); 

      if (pingReply.Status == IPStatus.Success) 
       result = true; 
     } 
     catch 
     { 
      result = false; 
     } 

     return result; 
    } 
+0

Thanx for u'r reply。但我的问题仍然存在。为什么我的线程不能在后台工作。整个项目一直下到名单完成ping!当线程完成其工作时,它不工作吗? –

+0

@maryammohammadi - 你是什么意思项目是失败?看看我的例子,你应该能够在foreach循环之后运行你想要的任何代码,并且ping会在不同的线程上发生。 – Omar

+0

@Omar,我怎样才能更新每个'label.Text'?我有一个在UI上的IP地址列表。 – Badiparmagi

0

考虑对fping实用程序进行系统调用。当然,这不是托管代码,但fping针对您的特定任务进行了很好的优化,并将问题简化为一次调用,然后处理基于文本的结果列表。

-1

您可以使用PowerShell

private string resultat(string s) 
{ 
    Runspace space = RunspaceFactory.CreateRunspace(); 
    space.Open(); 

    Pipeline pipeline = space.CreatePipeline(); 
    pipeline.Commands.AddScript(s); 
    pipeline.Commands.Add("Out-String"); 
    Collection<PSObject> results = pipeline.Invoke(); 

    StringBuilder stringBuilder = new StringBuilder(); 
    foreach (PSObject obj in results) 
    { 
     stringBuilder.AppendLine(obj.ToString()); 
    } 

    return stringBuilder.ToString(); 
} 

然后用resultat("ping -l 10 -n 2 " + your_ip);

相关问题