看下这段代码, 我开一个线程 处理完 10000条 要 13 秒 开 10 个线程 处理完 10000条要 21 秒 ,请问我要怎么改这段代码呢? //启动按钮
private void button1_Click(object sender, EventArgs e)
{
#region 读取
using (System.IO.StreamReader sr = new System.IO.StreamReader(this.txtRecePer.Text.Trim()))
{
while (!sr.EndOfStream)
{
al.Add(sr.ReadLine());
}
}
#endregion
this.lblShow.Text = "总数:" + al.Count.ToString();
int threadnum = Int32.Parse(txtThreads.Text.Trim());
#region 线程分配
th = new System.Threading.Thread[threadnum];
//线程分配
for (int j = 0; j < threadnum; j++)
{
th[j] = new System.Threading.Thread(new System.Threading.ThreadStart(SetValues));
th[j].Name = "线程" + j.ToString();
th[j].IsBackground = true;
th[j].Start();
}
#endregion
}
private void SetValues()
{
while(al.Count>0)
{
System.Threading.Monitor.Enter(al);
if (al.Count > 0)
{
string a = al[0].ToString();
if (this.rtbIng.InvokeRequired)
{
setControlValue scv = new setControlValue(InValues);
this.Invoke(scv, new object[] { System.Threading.Thread.CurrentThread.Name, a });
}
else
{
this.rtbIng.AppendText("[" + System.DateTime.Now + "]" + System.Threading.Thread.CurrentThread.Name + "--" + a + "\r\n");
}
al.RemoveAt(0);
}
else
{
break;
}
System.Threading.Monitor.Exit(al);
}
}
public void InValues(string name, string info)
{
this.rtbIng.AppendText("["+System.DateTime.Now+"]"+ name + "--" + info + "\r\n");
}
//停止
private void btnStop_Click(object sender, EventArgs e)
{
for (int i=0; i < th.Length; i++)
{
th[i].Abort();
}
}
private void button1_Click(object sender, EventArgs e)
{
#region 读取
using (System.IO.StreamReader sr = new System.IO.StreamReader(this.txtRecePer.Text.Trim()))
{
while (!sr.EndOfStream)
{
al.Add(sr.ReadLine());
}
}
#endregion
this.lblShow.Text = "总数:" + al.Count.ToString();
int threadnum = Int32.Parse(txtThreads.Text.Trim());
#region 线程分配
th = new System.Threading.Thread[threadnum];
//线程分配
for (int j = 0; j < threadnum; j++)
{
th[j] = new System.Threading.Thread(new System.Threading.ThreadStart(SetValues));
th[j].Name = "线程" + j.ToString();
th[j].IsBackground = true;
th[j].Start();
}
#endregion
}
private void SetValues()
{
while(al.Count>0)
{
System.Threading.Monitor.Enter(al);
if (al.Count > 0)
{
string a = al[0].ToString();
if (this.rtbIng.InvokeRequired)
{
setControlValue scv = new setControlValue(InValues);
this.Invoke(scv, new object[] { System.Threading.Thread.CurrentThread.Name, a });
}
else
{
this.rtbIng.AppendText("[" + System.DateTime.Now + "]" + System.Threading.Thread.CurrentThread.Name + "--" + a + "\r\n");
}
al.RemoveAt(0);
}
else
{
break;
}
System.Threading.Monitor.Exit(al);
}
}
public void InValues(string name, string info)
{
this.rtbIng.AppendText("["+System.DateTime.Now+"]"+ name + "--" + info + "\r\n");
}
//停止
private void btnStop_Click(object sender, EventArgs e)
{
for (int i=0; i < th.Length; i++)
{
th[i].Abort();
}
}
解决方案 »
- C#中如何打开位图文件然后继续写入一个位图文件
- C# 按引用传递值类型
- 如何获取2个时间的年差和月差
- Access数据库 备注型-更新时抱错?
- 求一日期类正则表达式写法~
- datagrid显示数据
- C# 如何对word文档内容进行解析(包括文本、公式、图片和表格)
- 找个成熟的美工兼职做个网站,有诚意的留下联系方式,谢谢
- 请教一个算法问题,有两个数组A,B,判断A中是否至少有一个元素和B中元素相同
- 在安装vs.net是时候,我怎么不能改变系统安装的路径啊?怎么所以东西都安装在系统盘,我根本改变不了,而的系统盘的空间又不够,怎么办?
- C# 函数内局部变量 或程序块内局部变量。
- 如何用C#绘制矢量图,并以矢量格式文件保存,以便于在AutoCAD等图形环境中修改和继续编辑。
多线程的目的是为了充分利用CPU,由于你做的是IO操作,因此无论你线程又多少,CPU始终需要等待IO操作完成,因此瓶颈在于IO操作,而非CPU。考虑从IO操作方面改进以提高性能。