高手赐教B/S模式多线程例子,谢谢

legu1 2011-11-28 12:51:56
这是我的代码,但是感觉没有缩短时间,有没有办法让两个线程异步,该如何做
protected void Button3_Click(object sender, EventArgs e)
{
Thread thread2 = new Thread(new ThreadStart(bind2));
Thread thread1 = new Thread(new ThreadStart(bind1));
thread1.Start();
thread2.Start();
// thread.Join(TimeSpan.FromSeconds(100));
Thread.CurrentThread.Join(TimeSpan.FromSeconds(100));

thread2.Abort();
thread1.Abort();
}
protected void bind1()
{
string ids = this.txtID.Text.Trim(); //父类ID
string str = this.DropDownList2.SelectedValue.Trim();
if (str == "父级id值")
{
ids = this.txtID.Text.Trim();
}
else if (str == "class")
{
ids = "";
}
string xpath = this.txtXPath.Text.Trim();//抓取链接的xpath
string url = this.txtUrl.Text.Trim();//网站的url

string pagePath = "";
string s = getstr(url, ids, pagePath, xpath);//获取结果值
this.txtLianJie.Text += s;
}
protected void bind2()
{
string ids = this.txtID.Text.Trim(); //父类ID
string str = this.DropDownList2.SelectedValue.Trim();
if (str == "父级id值")
{
ids = this.txtID.Text.Trim();
}
else if (str == "class")
{
ids = "";
}
string xpath = this.txtXPath.Text.Trim();//抓取链接的xpath
string url = this.txtUrl.Text.Trim();//网站的url

string pagePath = "";
string s = getstr(url, ids, pagePath, xpath);//获取结果值
this.txtLianJie.Text += s;
}
...全文
154 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
bdmh 2011-11-28
  • 打赏
  • 举报
回复
谁告诉你,线程就会缩短你的时间呢,调度线程要分配cpu时间,可能比你单线程更慢
jcl007_ 2011-11-28
  • 打赏
  • 举报
回复
多核的肯定可以
阿非 2011-11-28
  • 打赏
  • 举报
回复
3.5 需要安装一个扩展包

http://www.codeproject.com/KB/linq/TPL.aspx

4.0下可直接运行下面程序


int[] intArr = new int[10000];

Stopwatch sw = new Stopwatch();

sw.Start();
for (int i = 0; i < intArr.Length; i++)
{
System.Threading.Thread.Sleep(1);
}
sw.Stop();
Console.WriteLine("花费时间:{0}", sw.ElapsedMilliseconds);

sw.Restart();
Parallel.ForEach(intArr, i => { System.Threading.Thread.Sleep(1); });
sw.Stop();
Console.WriteLine("花费时间:{0}", sw.ElapsedMilliseconds);


CPU是四核的

运行耗时: 10689 vs 1976
legu1 2011-11-28
  • 打赏
  • 举报
回复
3.5
nfclass 2011-11-28
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 sandy945 的回复:]
.net framework 的版本是?
[/Quote]

-0.3
阿非 2011-11-28
  • 打赏
  • 举报
回复
.net framework 的版本是?
legu1 2011-11-28
  • 打赏
  • 举报
回复
双核的,我用的是asp.net ,b/S模式
阿非 2011-11-28
  • 打赏
  • 举报
回复
你用的什么开发环境? 是否多核CPU
legu1 2011-11-28
  • 打赏
  • 举报
回复
主要是我抓出友情链接,我还得把友情链接那些页面抓出来,去查,有没有我要的关键词,然后有关键词的再存入文本文档,所以比较费时
highpr 2011-11-28
  • 打赏
  • 举报
回复
支持创新
  • 打赏
  • 举报
回复
存数据可以用QueueUserWorker
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 legu1 的回复:]
我是做一个页面采集器,我如果用单线程来抓的话,一个页面有40条链接,我要抓 10分钟,所以我在想,如果我用两个线程来抓,会不会就会缩短这个时间
[/Quote]

不至于这么慢的吧
主线程负责抓取,后台线程存数据就OK了
legu1 2011-11-28
  • 打赏
  • 举报
回复
饿。。。。。。。。。。不明白,我只需要抓取页面上的链接,存到文本文档
阿非 2011-11-28
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 legu1 的回复:]
我是做一个页面采集器,我如果用单线程来抓的话,一个页面有40条链接,我要抓 10分钟,所以我在想,如果我用两个线程来抓,会不会就会缩短这个时间
[/Quote]
是否会缩短 要结合CPU和网卡的使用情况。

如果是多核的话,可以考虑PLinq
legu1 2011-11-28
  • 打赏
  • 举报
回复
我是做一个页面采集器,我如果用单线程来抓的话,一个页面有40条链接,我要抓 10分钟,所以我在想,如果我用两个线程来抓,会不会就会缩短这个时间
阿非 2011-11-28
  • 打赏
  • 举报
回复
你想做什么?

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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