//统计源文件行数
            string path = currentPath + sourceIPstring;
            if (!File.Exists(path))
            {
                MessageBox.Show("未找到对应的域名源文件,程序即将关闭,请确认源文件存在的情况下,打开本软件!");
                this.Close();
            }
            int count = 0;
            StreamReader countstream = new StreamReader(path);
            while (countstream.ReadLine() != null)
            {
                count++;
            }
            countstream.Close();            //初始化线程数据
            Thread[] mythread = new Thread[301];            //初始化进度条
            progressBar1.Minimum = 1;
            progressBar1.Maximum = count;
            progressBar1.Value = 1;            //读取域名
            int i = 0;
            StreamReader readstream = new StreamReader(path);
            string linestring = readstream.ReadLine();
            string domainName = "";
            string domainAddress = "";
            while (linestring != null)
            {
                try
                {
                    i++;
                    if (linestring.Length != 0)
                    {
                        int indexof = linestring.IndexOf(" ");
                        if (indexof <= 0)
                            indexof = linestring.Length;
                        domainName = linestring.Substring(0, indexof);
                        domainAddress = linestring.Substring(indexof, linestring.Length - indexof);                        int k = 300 - i;
                        if (k == 1)
                            i = 0;                        ping HostPing = new ping();                        //创建一个ping实例                         HostPing.fileString = currentPath + companyIPString;                        HostPing.domainName = domainName;                        HostPing.domainIPString = domainAddress;                        HostPing.ul = new UpdateList(UpdateMyList);                        t1 = mythread[k] = new Thread(new ThreadStart(HostPing.scan));                        //初始化一个线程实例                         mythread[k].Start();
                    }
                    linestring = readstream.ReadLine();
                }
                catch(Exception ex)
                {
                  // MessageBox.Show(ex.Message);
                }
            }
            readstream.Close();上面一段程序中,在读取文件达到 50K以上,就会出现内存溢出错误,小于50K时则一切正常,请高手 指点一下是否程序中某个地方没有释放内存造成,如果 是,请指点一二,谢谢大大们

解决方案 »

  1.   

    一个线程默认要保留1m的栈内存。而你这样不断的开线程,当然最终要爆了(改写但基本意思没变):while(  readstream.ReadLine() )
    {
       k = i++ % 301;
       t1 = mythread[k] = new Thread(new ThreadStart(HostPing.scan)); //<--?!
       mythread[k].Start(); 
    }
      

  2.   

    HostPing.scan  
    这个方法一直循环的?
      

  3.   

    while控制循环次数,当文件读取结束后,就不再调用线程了!
      

  4.   

    ping HostPing = new ping();
    囧你类名用小写,对象用大写的,这习惯
      

  5.   


                            int k = 300 - i; 
                            if (k == 1) 
                                i = 0; 
    我这个上面有控制线程数量呀!
      

  6.   


    是吗?你要是写成if(k==1) break;倒是可行。
      

  7.   

    使用BREAK不就终结了 while循环了吗?那后续的数据就无法处理了!
      

  8.   


    你也知道while循环没有停。
    那你不知道循环内的t1 = mythread[k] = new Thread(new ThreadStart(HostPing.scan));还在继续创建线程?
      

  9.   

    你是不是该把int count该为double count或long count呢?就是统计行数的地方。如果这么改了之后,那进度条的增加就不是这样的了。double valuePlusPerTime = (double)((long)1/count));
    double curValule = 0;//增加值的时候
    curValue += valuePlusPerTime;
    progress1.Value = (int)curValue;
      

  10.   

    我没看得出来这么写有什么问题。那个ping是个自定义类吧?有没有什么问题?内存溢出是.NET的OverFlowException吗?还是该内存为不可Read错误?什么地方出现的知道吗?
      

  11.   

    跟踪结果是 linestring = readstream.ReadLine(); 这句产生错误应该如gomoku 所说的因为线程在不断的开始,产生的溢出,ping  类是应该没有问题的!
      

  12.   

    引发类型为 “System.OutOfMemoryException”的异常。
      

  13.   

    WinForm报这个内存异常还真是少见呢,一般都是有足够内存的。可是为什么是在这个ReadLine的地方报错?你那个ping类的scan()方法是一个死循环吗?会自动结束的吗?你试下这样int k = 300 - i; 
    if (k == 1)

      i = 0; 
      if(myThread[i] != null)
          myThread[i].Abort();
    }