我的程序很简单,就是点击按钮就开始用多线程的方式 对网页list.aspx?id=0到999999的访问,如果网页中存在[我要的东西],就结束现在的问题是程序可以正常运行,就是速度太慢了(用100 MB的LAN运行也是一样),用Tcpview查看TCP连接时,开2线程或10线程总是2条,
我想问的时,有没有办法让它更快地检索网页的内容,即开10线程时 TCP连接也是用10条
private void button1_Click(object sender, System.EventArgs e)
{
// 一个一个地 启动线程
// ThreadTest b = new ThreadTest();
// Thread t = new Thread(new ThreadStart(b.runme));
// t.Start(); this.button1.Enabled=false;////
/// 批量启动 10 线程
////
threads = new Thread[threadNumber];
for (int i = 0; i < threadNumber; i++)
{
Thread t = new Thread(new ThreadStart( mytheard));
threads[i] = t;
threads[i].Name=i.ToString();
}
for (int i = 0; i < threadNumber; i++)
{
threads[i].Start();
} } /// <summary>
/// 获取 当前ID
/// </summary>
/// <returns></returns>
private double getMyID()
{
double temp;
lock (this)
{
temp=nowID;
nowID++;
} if(temp>9)
{
KillThread();
return 0;
}
else
{
return temp;
}
} /// <summary>
/// 结束线程
/// </summary>
private void KillThread()
{
for (int i = 0; i < threadNumber; i++)
{
threads[i].Abort();
}
this.button1.Enabled=true;
} /// <summary>
/// 线程函数
/// </summary>
private void mytheard()
{
while(hasGet)
{
double myID=getMyID();
string path = "http://www.baidu.com/?id=";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(path + myID);
Stream responseStream = ((HttpWebResponse) request.GetResponse()).GetResponseStream();
string str3 = new StreamReader(responseStream, Encoding.Default).ReadToEnd();
if (str3.IndexOf("我要的东西") > 0)
{
KillThread();
}
}
}
我想问的时,有没有办法让它更快地检索网页的内容,即开10线程时 TCP连接也是用10条
private void button1_Click(object sender, System.EventArgs e)
{
// 一个一个地 启动线程
// ThreadTest b = new ThreadTest();
// Thread t = new Thread(new ThreadStart(b.runme));
// t.Start(); this.button1.Enabled=false;////
/// 批量启动 10 线程
////
threads = new Thread[threadNumber];
for (int i = 0; i < threadNumber; i++)
{
Thread t = new Thread(new ThreadStart( mytheard));
threads[i] = t;
threads[i].Name=i.ToString();
}
for (int i = 0; i < threadNumber; i++)
{
threads[i].Start();
} } /// <summary>
/// 获取 当前ID
/// </summary>
/// <returns></returns>
private double getMyID()
{
double temp;
lock (this)
{
temp=nowID;
nowID++;
} if(temp>9)
{
KillThread();
return 0;
}
else
{
return temp;
}
} /// <summary>
/// 结束线程
/// </summary>
private void KillThread()
{
for (int i = 0; i < threadNumber; i++)
{
threads[i].Abort();
}
this.button1.Enabled=true;
} /// <summary>
/// 线程函数
/// </summary>
private void mytheard()
{
while(hasGet)
{
double myID=getMyID();
string path = "http://www.baidu.com/?id=";
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(path + myID);
Stream responseStream = ((HttpWebResponse) request.GetResponse()).GetResponseStream();
string str3 = new StreamReader(responseStream, Encoding.Default).ReadToEnd();
if (str3.IndexOf("我要的东西") > 0)
{
KillThread();
}
}
}
加上这个就可以 连接数200
HttpWebRequest request = (HttpWebRequest) WebRequest.Create(path + myID);
Stream responseStream = ((HttpWebResponse) request.GetResponse()).GetResponseStream();
string str3 = new StreamReader(responseStream, Encoding.Default).ReadToEnd(); if (str3.IndexOf("我要的东西") > 0)
{
KillThread();
}===========
把这部份优化只 只读取网页的<title> </title> 这部份不用整个网页代码全部下载回来分析