C#多线程调用WebClient速度变慢的问题,跪求高手帮忙

zhihuiqian 2010-01-15 04:56:14
不使用线程速度很快,创建多个线程序调用时就变得很慢,不知什么地方出问题了,跪求高手帮忙!
代码如下

private string getHtml(string url)
{
string restr = "";
WebClient myWebClient = new WebClient();
try
{
byte[] myDataBuffer = myWebClient.DownloadData(url);
restr = Encoding.Default.GetString(myDataBuffer);
myDataBuffer = null;
}
}
catch (Exception ex)
{
restr = "";
ErrlogIt("getHtml err:" + ex.Message);
}
finally
{
myWebClient.Dispose();
}
return restr;
}

private void putMO()
{
putmoBool = false;

DataSet dsMoUrl = new DataSet();
try
{
string sqlUrl = "select corpid,subnumber,mourl,moflag from corpurl where len(mourl)>10 and moflag<=0 order by moflag desc";
dsMoUrl = Query(sqlUrl);
}
catch (Exception ex)
{
ErrlogIt(ex.Message);
}

if (dsMoUrl == null)
{
putmoBool = true;
return;
}
if (dsMoUrl.Tables.Count == 0)
{
putmoBool = true;
return;
}

try
{
actionMo(dsMoUrl);
}
catch (Exception ex)
{
ErrlogIt(ex.Message);
}
finally
{

dsMoUrl.Dispose();
putmoBool = true;
}
}
private void actionMo(DataSet dsMoUrl)
{
bool canGo = true;
int ct = 0;
int i = 0;
ct = dsMoUrl.Tables[0].Rows.Count;
while (i < ct && canGo)
{
DataRow dr = dsMoUrl.Tables[0].Rows[i];
string corpid = dr["corpid"].ToString();
string subnumber = dr["subnumber"].ToString();
string mourl = dr["mourl"].ToString();
int moflag = int.Parse(dr["moflag"].ToString());

DataSet dsMoList = new DataSet();
if (canGo)
{
try
{
string QuerySql = "select top " + pushSpeed.ToString() + " * from mo.motocorp where corpID=" + corpid + " and subnumber=" + subnumber + " order by moflag asc,moid asc";
dsMoList = Query(QuerySql);
}
catch (Exception ex)
{
canGo = false;
ErrlogIt(ex.Message);
}
}

if (canGo)
{
int j = 0;
int listCt = dsMoList.Tables[0].Rows.Count;
string urlResp = "";
string respValue = "";

while (j < listCt && canGo)
{
DataRow listDr = dsMoList.Tables[0].Rows[j];
string moid = listDr["moid"].ToString();

string sUrl = mourl + "?msgFormat=1&moID=" + moid

try
{
urlResp = getHtml(sUrl);
}
catch
{
urlResp = "";
logIt("失败!");
this.labErr.Text = (int.Parse(this.labErr.Text.ToString()) + 1).ToString();
}

try
{
if (urlResp.Length < 3)
urlResp = urlResp + " ";

respValue = urlResp.Substring(0, 3);
if (respValue.Trim() == "100")
{
logIt("moOK!");
}
else
{
logIt("moNO!");
canGo = false;
}
}
catch (Exception ex)
{
ErrlogIt(ex.Message);
}

j = j + 1;
}

}
dsMoList.Dispose();
i = i + 1;
}
}


程序有多个TIMER,并且都有调用getHtml(string url) 方法,如果不使用线程速度很快,使用超过两个线程就变慢了
...全文
737 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
cai123321cai 2011-08-01
  • 打赏
  • 举报
回复
Program.cs

static void Main()
{
System.Net.ServicePointManager.DefaultConnectionLimit = 255; //加上这句

默认webquest只允许2个连接
winner2050 2010-09-25
  • 打赏
  • 举报
回复
只能用排除法了。

你去掉读取数据库的功能,去掉采集html的功能,看看速度怎么样。

这样来找瓶颈。
lyzawd 2010-09-24
  • 打赏
  • 举报
回复
多线程的使用主要是看资源的并发使用性上,如果程序的瓶颈在可并发的资源上,那么就可以使用多线程,如果资源是单并发的,那么只能使用单线程,多线程反而会增加开销,降低性能。

比如楼主的性能瓶颈如果在gethtml上,那么由于网络资源大部分是可并发的,并发访问,就能提高性能。
而如果楼主的瓶颈在数据库那块,那单表的更新操作是排他的,单靠多线程是没有任何意义的。
myjavamosquito 2010-09-21
  • 打赏
  • 举报
回复
用sleep(时间)将线程停下,免得多个线程打架。。
By_Soul_Cc 2010-07-27
  • 打赏
  • 举报
回复
的确是高手啊! 顶起
喂大的程序员 2010-07-23
  • 打赏
  • 举报
回复
老有兴趣了,顶顶
zhouxingyu896 2010-07-21
  • 打赏
  • 举报
回复
用线程池去管理线程
WGH12345678 2010-04-23
  • 打赏
  • 举报
回复
顶起,真是高手啊!
zsuswy 2010-04-14
  • 打赏
  • 举报
回复
没有看到你的程序里面有任何的一句关于线程的代码哦。。。。。。
mayonglong 2010-03-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 jing644724116 的回复:]
仔细再看看
[/Quote]

????
jing644724116 2010-03-02
  • 打赏
  • 举报
回复
adnin2010 2010-03-02
  • 打赏
  • 举报
回复
顶起
aboluoyuren 2010-01-26
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 ouc_ajax 的回复:]
如果你的任务全部是计算密集型的,在没有多CPU的情况下,是反而慢了,因为增加了线程调度的开销,如果全部是I/O密集的,那速度和单线程没有什么区别,因为慢是在I/O了,如果是计算密集和I/O密码的都用,可能还有一些效果。

多线程不适合楼主的这种方法,而是适用于一个线程任务少(例如只等待响应用户按键),另外一个线程密集型的情况
[/Quote]

赞一下
ouc_ajax 2010-01-18
  • 打赏
  • 举报
回复
如果你的任务全部是计算密集型的,在没有多CPU的情况下,是反而慢了,因为增加了线程调度的开销,如果全部是I/O密集的,那速度和单线程没有什么区别,因为慢是在I/O了,如果是计算密集和I/O密码的都用,可能还有一些效果。

多线程不适合楼主的这种方法,而是适用于一个线程任务少(例如只等待响应用户按键),另外一个线程密集型的情况
zhihuiqian 2010-01-15
  • 打赏
  • 举报
回复
自已顶下

1,979

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 其他语言讨论
社区管理员
  • 其他语言社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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