using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;
using System.Collections;
using System.IO;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();     
错误出现在这里

解决方案 »

  1.   

    数组集合从0开始的,无论你的Min从5,还是10,还是100,建立的mythread数组永远是0开始,所以当你min为非0时,就会越界了
    代码自己改,多用断点,多调试,基础一定要打好
      

  2.   

    i <= Max  替换成  i < Max
      

  3.   

    i <= Max  替换成  i < Max
      

  4.   

    for (i = Min; i <= Max; i++)
                {
                    int k = Max-1;
    这里改为  int k= i- Min;这样就能够使用