读书人

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

发布时间: 2012-01-16 23:36:51 作者: rapoo

高手赐教B/S模式多线程例子,谢谢
这是我的代码,但是感觉没有缩短时间,有没有办法让两个线程异步,该如何做
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;
}

[解决办法]
谁告诉你,线程就会缩短你的时间呢,调度线程要分配cpu时间,可能比你单线程更慢
[解决办法]

探讨
我是做一个页面采集器,我如果用单线程来抓的话,一个页面有40条链接,我要抓 10分钟,所以我在想,如果我用两个线程来抓,会不会就会缩短这个时间

[解决办法]
探讨
我是做一个页面采集器,我如果用单线程来抓的话,一个页面有40条链接,我要抓 10分钟,所以我在想,如果我用两个线程来抓,会不会就会缩短这个时间

[解决办法]
你用的什么开发环境? 是否多核CPU
[解决办法]
3.5 需要安装一个扩展包

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

4.0下可直接运行下面程序

C# code
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);
[解决办法]
多核的肯定可以

读书人网 >asp.net

热点推荐