[:(!]写了一个多线程下载网业内容的程序,下载正常,就是程序隔一会CPU占用就到90%(50分)

  • 主题发起人 主题发起人 onyliu
  • 开始时间 开始时间
O

onyliu

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TGetMM.Execute;
var
strm:TStringStream;
IdGetMM: TIdHTTP;
url_str,sqlstr,str_temp:string;
count,i : integer;
source,str_url,str_date,str_registdate : string;
ADOQuery: TADOQuery;
begin
try//-------------1
//IdGetMM :=TIdHTTP.Create(nil);
sleep(5);
begin
//1
while 1>0do
begin
//2
for count:=strtoint(FPagebegin
) to StrToInt(FpageEnd)do
begin
try//------------------3
if IdGetMM <> nil then
IdGetMM.Free;
IdGetMM :=TIdHTTP.Create(nil);
if ADOQuery <> nil then
ADOQuery.Free;
ADOQuery:= TADOQuery.Create(nil);
sleep(10);
ADOQuery.ConnectionString :=ConnStr;
if FUrlInfo <> nil then
FUrlInfo.Free;
FUrlInfo := TStringList.Create;
if FRegistdate <> nil then
FRegistdate.Free;
FRegistdate := TStringList.Create;
sleep(10);
url_str := FUrl + inttostr(count);
try//-------------4
sleep(10);
strm :=TStringStream.Create('');
IdGetMM.Get(url_str,strm);
source := strm.DataString;
finally
strm.Free;
end;
//4
sleep(5);
if Fbegin
key <> '' then
source := FormTCutString.GetstrFromHTML(Fbegin
key,FEndkey,source);
if FDelkey1 <> '' then
source := FormTCutString.StrReplace(FDelkey1,'',FDelkey2,'',source);
source := FormTCutString.StrReplace('&quot;','','''','',source);
str_url := FormTCutString.StrReplace('<a href=',FMainKey,'<a href=',FMainKey,FSplit,'汗',source);
if Fdatekey1 <> '' then
source := FormTCutString.StrReplace(Fdatekey1,Formatdatetime('YYYY',Now)+'-',Fdatekey2,'-',source);
str_temp := source;
str_date := FormTCutString.ParseDate(str_temp,1);
str_date := str_date+FormTCutString.ParseDate(str_temp,2);
FormTCutString.SeparateTerms(str_date,',',FRegistdate);
str_url := FormTCutString.Parseurl(str_url,FSubKey);
if str_url <>'' then
begin
//4
FormTCutString.SeparateTerms(str_url,',',FUrlInfo);
for i := 0 to FUrlInfo.Count-1do
begin
//5
if pos('xxxx',FUrlInfo.Strings)= 0 then
begin
//6
sleep(10);
ADOQuery.SQL.Clear;
sqlstr:='select * from rentinfo where registdate='+''''+Formatdatetime('YYYY-MM-DD',Now)+''''+' and url ='+''''+FUrlInfo.Strings+'''';
ADOQuery.SQL.Text := sqlstr;
ADOQuery.Open;
if ADOQuery.RecordCount =0 then
//--判断URL是否重复
begin
//7
try//-------------5
sleep(10);
strm :=TStringStream.Create('');
sleep(10);
url_str := FUrlInfo.Strings;
IdGetMM.Get(url_str,strm);
source := strm.DataString;
finally
strm.Free;
end;
//5
if Trim(FSubbegin
key) <> '' then
source := FormTCutString.GetstrFromHTML(FSubbegin
key,FSubEndkey,source);
sleep(10);
source := Clear(source);
str_registdate := '';
if FRegistdate.Count > 0 then
str_registdate := FRegistdate.Strings;
if str_registdate = '' then
begin
if (pos(Formatdatetime('YYYY-MM-DD',Now),source)>0) or (pos(Formatdatetime('YYYY-M-DD',Now),source)>0) or (pos(Formatdatetime('MM-DD',Now),source)>0) or (pos(Formatdatetime('M-DD',Now),source)>0) then
str_registdate := Formatdatetime('YYYY-MM-DD',Now);
end;
if str_registdate = Formatdatetime('YYYY-MM-DD',Now) then
begin
ADOQuery.SQL.Clear;
sqlstr:='select * from rentinfo where registdate='+''''+Formatdatetime('YYYY-MM-DD',Now)+''''+' and telephone='+''''+Get(source,TelephoneList)+''''+' and area='+''''+Get(source,AreaList)+'''';
ADOQuery.SQL.Text := sqlstr;
sleep(10);
ADOQuery.Open;
if ADOQuery.RecordCount =0 then
//--判断是否重复数据
begin
//8
//--判断黑名单
ADOQuery.SQL.Clear;
sqlstr:='select * from rentblacktel where '''+Get(source,TelephoneList)+''' like '+'''%''+replace(telephone,'+''' '''+','+''''''+')+'+'''%''';;
ADOQuery.SQL.Text := sqlstr;
sleep(10);
ADOQuery.Open;
if (ADOQuery.RecordCount =0) then
begin
sqlstr :='insert into rentinfo(registdate,Buildtype,Structure,Tracffic,Contact,SellType,base,equipment,mright,fitment,other,url,urltype,siteid,cityid,district,area,provider,telephone,floor,size,room,buildyear,towards,price) values(';
sqlstr :=sqlstr +''''+str_registdate+''''+','+''''+Get(source,BuildtypeList)+''''+','+''''+Get(source,StructureList)+''''+','+''''+Get(source,TracfficList)+''''+','+''''+copy(Get(source,ContactList),0,6)+''''+','+''''+Get(source,SellTypeList)+''''+',';
sqlstr :=sqlstr +''''+Get(source,BaseList)+''''+','+''''+Get(source,EquipmentList)+''''+','+''''+Get(source,MrightList)+''''+','+''''+Get(source,FitmentList)+''''+','+''''+Get(source,OtherList)+''''+',';
sqlstr :=sqlstr +''''+FUrlInfo.Strings+''''+','+FUrltype+','+FSiteID+','+FCityID+','+''''+Get(source,DistrictList)+''''+','+''''+Get(source,AreaList)+''''+','+'''个人'''+','+''''+Get(source,TelephoneList)+''''+','+''''+Get(source,FloorList)+''''+','+''''+Get(source,SizeList)+''''+','+''''+Get(source,RoomList)+''''+','+''''+Get(source,YearList)+''''+','+''''+Get(source,TowardsList)+''''+','+''''+Get(source,PriceList)+''''+')';
ADOQuery.SQL.Clear;
ADOQuery.SQL.Text := sqlstr;
sleep(10);
ADOQuery.ExecSQL;
end;
end;
//8----
end;
end;
//7---
end;
//6-----
end;
//4--
except
end;
//try3
end;
//3--
end;
//2--
sleep(StrTOInt(FSleepTime));
end;
//1---
finally
IdGetMM.Free;
end;
//try1
end;
 
……你这个代码看起来真让人头痛,你不会分成几个函数或过程来写啊?看看都头痛啊~
 
你是不是少了一句??线程运行完毕不关闭,一直挂起的吧??
FreeOnTerminate:=True;
 
程序是一直运行的,
循环一次
就sleep(StrTOInt(FSleepTime))一下
要说创建的话 也就下面几个
if IdGetMM <> nil then
IdGetMM.Free;
IdGetMM :=TIdHTTP.Create(nil);
if ADOQuery <> nil then
ADOQuery.Free;
ADOQuery:= TADOQuery.Create(nil);
sleep(10);
ADOQuery.ConnectionString :=ConnStr;
if FUrlInfo <> nil then
FUrlInfo.Free;
FUrlInfo := TStringList.Create;
if FRegistdate <> nil then
FRegistdate.Free;
FRegistdate := TStringList.Create;
sleep(10);
一共开了10个线程不到啊
 
使用WinInet单元来下载网页内容比较好些
InternetOpen
InternetOpenUrl
InternetReadFile
InternetCloseHandle
 
谢谢
To JeLiY 有时间我会看看 WinInet单元的
但目前先解决这个问题吧[:)]
因为除了占用CPU外 便没问题了
TO cb20000 我开了个新贴 整理了下程序,再帮忙看看[:D]
http://www.delphibbs.com/delphibbs/dispq.asp?lid=3599370
 
这帖子还是先结了,谢谢关注[:)]
 
多人接受答案了。
 
后退
顶部