C#多线程问题!求助!!!在线等!!!!!

hack大灯 2012-05-29 05:39:34
首先,我通过按钮启动多个线程,循环启动,但是每次都是只能运行完成2个线程

private void button1_Click_1(object sender, EventArgs e)
{
this.timer1.Enabled = true;
this.Paraments = this.textBox1.Text;
this.Cookies = this.textBox2.Text;
if (this.dt.Rows.Count == 0)
{
for (int i = begin; i <= end; i++)
{

DataRow dr = dt.NewRow();
dr["URL"] = this.text_url.Text.Replace("(*)", i.ToString());
dr["id"] = i.ToString();
this.dt.Rows.Add(dr);
}
this.dataGridView1.Refresh();
}
StopThread(Go_threads);

for (int i = 0; i < this.dt.Rows.Count; i++)
{
Go_threads[i] = new Thread(new ParameterizedThreadStart(Go_Thread));
Go_threads[i].Name = "thread_" + i.ToString();
Go_threads[i].Start(i);
}

}
private void Go_Thread(object index)
{
int i = int.Parse(index.ToString());
string Url = dt.Rows[i]["URL"].ToString();
string parament = Paraments;
parament = parament.Replace("(*)", dt.Rows[i]["id"].ToString());
string cookies = Cookies;
HttpManager hm = new HttpManager(Url,parament,cookies);
string res = "";

res = hm.Post();
string html = hm.Data;
if (html != string.Empty)
{
if (html.Contains("文章不存在"))
{
res = "HasNotpage";
}
}
dt.Rows[i]["FLAG"] = res;


}


我加入了一个timer,然后设置5秒检测一次线程数组,如果数组没有running线程的话,就新开下一组线程,但是我发现在这个时候,Thread.start()失效了!!!!求解啊啊啊啊啊啊啊!!!!

private void timer1_Tick(object sender, EventArgs e)
{
bool isOver = true;
for (int i = 0; i < Test_threads.Length; i++)
{
if (Test_threads[i] != null)
{
if (Test_threads[i].IsAlive)
{
if (Test_threads[i].ThreadState != ThreadState.Stopped)
{
isOver = false;
break;
}
}
}
}
for (int i = 0; i < Go_threads.Length; i++)
{
if (Go_threads[i] != null)
{
if (Go_threads[i].IsAlive)
{
if (Go_threads[i].ThreadState != ThreadState.Stopped)
{
isOver = false;
break;
}
}
}
}
if (isOver)
{
this.text_begin.Text = (int.Parse(this.text_begin.Text) - 2).ToString();

dt.Clear();
if (this.dt.Rows.Count == 0)
{
for (int i = begin; i <= end; i++)
{

DataRow dr = dt.NewRow();
dr["URL"] = this.text_url.Text.Replace("(*)", i.ToString());
dr["id"] = i.ToString();
this.dt.Rows.Add(dr);
}
this.dataGridView1.Refresh();
}
StopThread(Go_threads);

for (int i = 0; i < this.dt.Rows.Count; i++)
{
Go_threads[i] = new Thread(new ParameterizedThreadStart(Go_Thread));
Go_threads[i].Name = "thread_" + i.ToString();
Go_threads[i].Start(i);
}
}
...全文
206 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sd1232123 2012-05-30
  • 打赏
  • 举报
回复
假设你的datatable初始有很多个datarow,那就有几个 就起N+1个线程咯,看不到全部代码,我猜测应该你默认是空的
sd1232123 2012-05-30
  • 打赏
  • 举报
回复
给分把,不然我会很不开心的
sd1232123 2012-05-30
  • 打赏
  • 举报
回复
if (this.dt.Rows.Count == 0)

之后又在这个判断下加了一个datarow,那么就是datatable有一行对吧?之后你又对这个datatable的行进行遍历, for (int i = 0; i < this.dt.Rows.Count; i++) 那么正好是起2个线程。。。对于你的想法我不是很清楚
CSharpProgrammer 2012-05-30
  • 打赏
  • 举报
回复
楼主,我不知道你的DataTable 初始化的时候是否有超过2个以上的列,如果确认不是
for (int i = 0; i < this.dt.Rows.Count; i++) 这个计数器地方的问题,我建议你在启动线程的时候,给线程的 IsBackgroued 属性设置为True,同时在启动线程中间都加上 System.Threading.Thread.Sleep(1);这个语句,会有想不到的效果。

for (int i = 0; i < this.dt.Rows.Count; i++)
{
Go_threads[i] = new Thread(new ParameterizedThreadStart(Go_Thread));
Go_threads[i].Name = "thread_" + i.ToString();
Go_threads[i].Start(i);
System.Threading.Thread.Sleep(1);//增加代码
}

其实你的线程操作逻辑看上去基本上思路都是对的,问题是你是放到了ASP.NET 的页面中去执行,所以还要考虑页面生命周期的问题。
hack大灯 2012-05-29
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

看起来好像是线程数组重复赋值了
[/Quote]
不是啊!我重新new也不管用,还是那样的情况……

this.Go_threads = new Thread[200];
StopThread(Go_threads);

for (int i = 0; i < this.dt.Rows.Count; i++)
{
Go_threads[i] = new Thread(new ParameterizedThreadStart(Go_Thread));
//Go_threads[i].Name = "thread_" + i.ToString();
Go_threads[i].Start(i);
}
lkr2380869 2012-05-29
  • 打赏
  • 举报
回复
看起来好像是线程数组重复赋值了
hack大灯 2012-05-29
  • 打赏
  • 举报
回复
顶上去求大神啊……
hack大灯 2012-05-29
  • 打赏
  • 举报
回复
顶顶!!顶!顶!顶!顶!顶!顶!顶!顶!顶!顶!顶!顶!顶!顶!顶!顶!求大神!急死了啊啊啊 啊!!!!!!!!!!!!
hack大灯 2012-05-29
  • 打赏
  • 举报
回复
顶起来啊!!!!求大神解决啊!!!!!

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧