【多线程】用多线程生成静态页提高效率的问题
公司做一个cms系统,我负责生成静态这一块。功能都做了,但是生成速度比较慢,想到多线程可以提高效率。所以就弄上来,结果出现了一些问题(多线程还没完全搞懂),
一个是在子线程里面调用进度条功能,报错
还有一个是会发生两个子线程同时操作同一个文件(生成同一篇文章)报错
好了代码贴一下,求大神指点
/// <summary>多线程??.net?生成静态
/// 生成文章内容
/// </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();
}
}