代码同上: 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
for (int i = 0; i < 255; i++)
{
//当前需要Ping的IP
string strIP = strGateWay + i.ToString(); if (IsPingIP(strIP))
{
//添加至在线主机集合
arrOnline.Add(strIP);
}
}
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();
}
改为
Console.WriteLine(ips[(int)_] + ":" + pr.Status);获取请求地址的回应。
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;
}
}
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);
}
500个空着的,就给我分了9个
threadPing函数内无法强制类型噢。
int index = (int)((object[])ip)[1];这一句。
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为止!
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为止!
{
int result = await pingip(ip[i]);
}
这代码您测试过了吗?我测试不行,全部变成成功了。thisThread.Start的是sendPara 不是strIP
{
Thread.Sleep(一万年);
}
嗯,的确ping的效率不好。你有什么方法去更快实现这个。