use*_*202 6 c# multithreading network-programming backgroundworker
我有一个应用程序,它会ping本地子网上的每个可能的IP,以便编译响应的IP地址列表.目前,它一次ping所有255个.是否可以通过一次ping多个线程来转换此应用程序以使用多个线程来提高速度?我是多线程概念的新手,并认为这是一个很好的学习方法(只要当然可能).
此外,您可以教育我的任何风格改进也会有所帮助.
提前谢谢
这是backgroundWorker1_DoWork事件中的当前ping方法.
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
count = 0;
for (int i = 1; i < 255; i++)
{
Ping ping = new Ping();
PingReply pingreply = ping.Send(IPAddress.Parse(locip[0] + "." + locip[1] + "." + locip[2] + "." + i));
count += 1;
if (pingreply.Status == IPStatus.Success)
{
status = "o";
repAddress = pingreply.Address.ToString(); ;
repRoundtrip = pingreply.RoundtripTime.ToString();
repTTL = pingreply.Options.Ttl.ToString();
repBuffer = pingreply.Buffer.Length.ToString();
string[] lineBuffer = { status, repAddress, repRoundtrip, repTTL, repBuffer };
ipList.Rows.Add(lineBuffer);
}
progressBar.Invoke(new MethodInvoker(UpdateProgressBarByOne));
progressStatus.Text = ("Pinging IP " + count + " of 254");
}
button1.Enabled = true;
progressBar.Invoke(new MethodInvoker(ResetProgressBar));
}
Run Code Online (Sandbox Code Playgroud)