线程扫描IP地址段 错误:索引超出了数组界限。
程序代码:using System;
using System.Collections.Generic;
using using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using using using System.Collections;
using namespace 使用线程扫描计算机
{
public delegate void UpdateList(string sIP, string sHostName);
public partial class Form1 : Form
{
private System.DateTime StartTime;
public Form1()
{
InitializeComponent();
Form1.CheckForIllegalCrossThreadCalls = false;
}
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
StartTime = DateTime.Now;
string mask = numericUpDown1.Value.ToString() + "." + numericUpDown2.Value.ToString() + "." + numericUpDown3.Value.ToString() + ".";
int Min = (int)numericUpDown4.Value;
int Max = (int)numericUpDown5.Value;
if (Min > Max)
{
MessageBox.Show("输入地址取件不合法", "错误");
return;
}
int _ThreadNum = Max - Min + 1;
Thread[] mythread = new Thread[_ThreadNum];
progressBar1.Minimum = Min;
progressBar1.Maximum = Max;
int i;
for (i = Min; i <= Max; i++)
{
int k = Max-1;
ping HostPing = new ping();
HostPing.ip = mask + i.ToString();
HostPing.ul = new UpdateList(UPdateMyList);
mythread[k] = new Thread(new ThreadStart(HostPing.scan));
mythread[k].Start();
}
}
public class ping
{
public UpdateList ul;
public string ip;
public string HostName;
public void scan()
{
IPAddress myIP = IPAddress.Parse(ip);
try
{
IPHostEntry myHost = Dns.GetHostEntry(myIP);
HostName = myHost.HostName.ToString();
}
catch
{
HostName = "";
}
if (HostName == "")
HostName = "主机没有响应!";
if (ul != null)
ul(ip, HostName);
}
}
void UPdateMyList(string sIP, string sHostName)
{
lock (listBox1)
{
listBox1.Items.Add(sIP + " " + sHostName);
if (progressBar1.Value != progressBar1.Maximum)
{
progressBar1.Value++;
}
if (progressBar1.Value == progressBar1.Maximum)
{
MessageBox.Show("成功完成检测!", "提示");
DateTime EndTime = DateTime.Now;
TimeSpan ts = EndTime - StartTime;
label4.Text = ts.Seconds.ToString() + "秒";
progressBar1.Value = progressBar1.Minimum;
}
}
}
}
}mythread[k] = new Thread(new ThreadStart(HostPing.scan));
mythread[k].Start(); 错误提示:索引超出了数组界限。
该怎么解决。。。。。







