日期:2014-05-17  浏览次数:20526 次

【多线程】用多线程生成静态页提高效率的问题
公司做一个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 thre