public void run()
    {
        Stream resStream = null;
        StreamReader sr = null;
        StreamWriter sw = null;
        for (int i = 1; i <= Count; i++)
        {
            string PageUrl = "http://www.baidu.com";
            string strRelPath = (OkCount + 1) + ".html";
            //你要生成的ASPX文件
            try
            {
                System.Net.WebRequest request = System.Net.WebRequest.Create(PageUrl);
                WebResponse response = request.GetResponse();
                resStream = response.GetResponseStream();
                sr = new StreamReader(resStream);                sw = new StreamWriter(MapPath("html\\") + strRelPath, false, code);
                sw.Write(sr.ReadToEnd());
                _okCount++;
                System.Threading.Thread.Sleep(2);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                sw.Close();
                sr.Close();
                resStream.Close();
            }
        }
    }多线程生成html时老是出现如题的错误,有时候是其他文件,比如3.html不知道是什么问题,请大家来帮忙,需要怎么修改,谢谢。

解决方案 »

  1.   


    我的文件名是这个变量string strRelPath,不知道是不是多请求了一次
    后台调用
     protected void Button1_Click(object sender, EventArgs e)
        {
            ToHtml toheml = new ToHtml();
            Session["okcount"] = toheml;
            sum = toheml.Count;
            this.DataBind();
            Thread t1 = new Thread(new ThreadStart(toheml.run));
            Session["tempThread"] = t1;
            t1.Start();
            this.RegisterStartupScript("a", "<script language=\"javascript\">var timers=setInterval(\"Invoke();\", 500);</script>");
            this.Button1.Enabled = false; 
            
        }
      

  2.   


    需要怎么修改啊,我关闭都写在finally里面的,不行吗?
      

  3.   

    你的OkCount 什么时候累加的?再说了多个线程中OkCount +1命名是不行的,最好用guid
      

  4.   


    我的OKcount是在run方法里累加的具体类:
    public class ToHtml
    {
        int _count;
        int _okCount;
        Encoding code = Encoding.UTF8;    public int Count
        {
            get
            {
                return _count;
            }
        }
        public int OkCount
        {
            get
            {
                return _okCount;
            }
        }    public ToHtml()
        {
            _count = 10;
            _okCount = 0;
        }    public void run()
        {
            Stream resStream = null;
            StreamReader sr = null;
            StreamWriter sw = null;
            for (int i = 1; i <= Count; i++)
            {
                string PageUrl = "http://www.baidu.com";
                string strRelPath = (OkCount + 1) + ".html";
                //你要生成的ASPX文件
                try
                {
                    System.Net.WebRequest request = System.Net.WebRequest.Create(PageUrl);
                    WebResponse response = request.GetResponse();
                    resStream = response.GetResponseStream();
                    sr = new StreamReader(resStream);                sw = new StreamWriter(MapPath("html\\") + strRelPath, false, code);
                    sw.Write(sr.ReadToEnd());
                    _okCount++;
                    System.Threading.Thread.Sleep(2);
                }
                catch (Exception e)
                {
                    throw e;
                }
                finally
                {
                    sw.Close();
                    sr.Close();
                    resStream.Close();
                }
            }
        }
    }
      

  5.   

    _okCount++;
    OkCount 是什么东西???