高手赐教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时间,可能比你单线程更慢
------解决方案--------------------
------解决方案--------------------
------解决方案--------------------你用的什么开发环境? 是否多核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);
------解决方案--------------------
多核的肯定可以