首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

【多线程】用多线程生成静态页提高效率的有关问题

2013-10-09 
【多线程】用多线程生成静态页提高效率的问题公司做一个cms系统,我负责生成静态这一块。功能都做了,但是生成

【多线程】用多线程生成静态页提高效率的问题
公司做一个cms系统,我负责生成静态这一块。功能都做了,但是生成速度比较慢,想到多线程可以提高效率。所以就弄上来,结果出现了一些问题(多线程还没完全搞懂),

一个是在子线程里面调用进度条功能,报错

还有一个是会发生两个子线程同时操作同一个文件(生成同一篇文章)报错

好了代码贴一下,求大神指点

/// <summary>
        /// 生成文章内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void CreatNews(string ClassId)
        {
            string id = this.ddlClass.SelectedValue;
            string where = id == "0" ? "" : (" n.Classid in(" + ClassId + ")");

            int k = 0;
            int r = 0, p = 0;
            DataTable dts = new TCms.DAL.News().BindPage2(where, 1, 10000, out r, out p);
            k = p;

            TCms.Common.HProgressBar.Start();
            createVo = new TCms.Common.CreateHtmlVo();
            createVo.Count = r;
            dts.Dispose();


            for (int i = 1; i <= k; i++)
            {
                dt_news = new TCms.DAL.News().BindPage2(where, i, 10000, out r, out p);

                Thread thread1 = new Thread(new ThreadStart(create));
                Thread thread2 = new Thread(new ThreadStart(create));
                Thread thread3 = new Thread(new ThreadStart(create));
                Thread thread4 = new Thread(new ThreadStart(create));
                Thread thread5 = new Thread(new ThreadStart(create));
                Thread thread6 = new Thread(new ThreadStart(create));
                Thread thread7 = new Thread(new ThreadStart(create));
                Thread thread8 = new Thread(new ThreadStart(create));
                Thread thread9 = new Thread(new ThreadStart(create));
                Thread thread10 = new Thread(new ThreadStart(create));

                thread1.Start();
                thread2.Start();
                thread3.Start();
                thread4.Start();
                thread5.Start();
                thread6.Start();
                thread7.Start();
                thread8.Start();
                thread9.Start();
                thread10.Start();

            }

            if (createVo.Count == createVo.Succount)
            {
                Toop = "<script>alert('生成成功');location.href=location.href;</script>";
            }


        }
        protected void create()
        {
            string resultstr = "";
            for (; s < dt_news.Rows.Count; s++)
            {
                DataRow dr = dt_news.Rows[s];
                Thread.Sleep(1);
                TCms.CreateHtml.CreateHtml create = new TCms.CreateHtml.CreateHtml();
                create.SavePath = AppDomain.CurrentDomain.BaseDirectory + dr["PageUrl"];
                create.FileName = dr["id"] + ".html";
                create.TempPath = AppDomain.CurrentDomain.BaseDirectory + "/temp/show.html";
                create.ClassId = Convert.ToInt32(dr["ClassId"].ToString());
                create.NewsId = Convert.ToInt32(dr["ID"].ToString());
                create.DR = dr;
                resultstr = create.GenerateHtml();
                
            }
        }

多线程??.net?生成静态
[解决办法]
for (; s < dt_news.Rows.Count; s++) 
//这个s的取值会有问题、100个文件要生成,10个线程每个生成10个,1-10,20-30。。。 
DataRow dr = dt_news.Rows[1-10];DataRow dr = dt_news.Rows[20-30];

热点排行