多线程下载问题--线程是运行完即释放好 还是创建后不释放等待参数?已创建线程如何接受新参数???(100分)

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

onyliu

Unregistered / Unconfirmed
GUEST, unregistred user!
[:(]那位大哥能为小弟解答下,最好给个例子!
我是用ADO 和IDHTTP 下载信息。
 
1.看你的需求
2.发消息、全局变量
 
----原文
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Cut, IdCookieManager, IdAntiFreezeBase, IdAntiFreeze,
IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP,
StdCtrls, DB, ADODB, Grids, DBGrids, ExtCtrls, ActiveX;
type
TForm1 = class(TForm)
Button3: TButton;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Edit9: TEdit;
Edit10: TEdit;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
RGet:TThread;
procedure InitList();
procedure GetMMThread(ID,Url,CityID,UrlType,Siteid:TStringList;Sqlstr:string);
public
{ Public declarations }
end;
//线程类
type
TGetMM = class(TThread)
private
//procedure decTcount;
//procedure incTcount;
FID:TStringList;
FUrl:TStringList;
FCityID :TStringList;
FUrltype :TStringList;
FSiteid :TStringList;
FSource :string;
FSqlstr:string;
FADOQuery : TADOQuery;
Fstrm:TStringStream;
FIdGetMM: TIdHTTP;
function Clear(source: string): string;
function Get(source: string;TempList : TStringList): string;
protected
procedure Execute;override;
public
constructor Create(ID,Url,CityID,urltype,Siteid:TStringList;sqlstr:string);
end;

var
Form1: TForm1;
FormTCutString : TCutString;
//tcount :integer;
maxcount : integer;
//flag : boolean;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
FormTCutString := TCutString.Create;
FormTCutString.iniFileInit;
FormTCutString.InitUrlList;
InitList();
//Memo1.Lines.Add('初始化成功。。。');
//sleep(40000);
//Button3.Click;
end;

procedure TForm1.InitList;
var
sqlstr :string;
i : integer;
InitQuery: TADOQuery;
begin
try
InitQuery := TADOQuery.Create(nil);
InitQuery.ConnectionString := ConnStr;
with InitQuerydo
begin
i :=0;
sqlstr :='update urlinfo set online=2 where id in (select top 5 id from urlinfo where online=0)';
InitQuery.SQL.Clear;
InitQuery.SQL.Text := sqlstr;
InitQuery.ExecSQL;
sleep(1000);
sqlstr := 'select top 5 id,url,urltype,cityid,siteid from urlinfo where online=2';
//where urltype='+Urltype+' and cityid ='+CityID+' order by id';
InitQuery.SQL.Clear;
InitQuery.SQL.Text := sqlstr;
try
InitQuery.Open;
while not InitQuery.Eofdo
begin
IDList.Insert(i,InitQuery.FieldByName('id').AsString);
UrlList.Insert(i,InitQuery.FieldByName('url').AsString);
UrltypeList.Insert(i,InitQuery.FieldByName('urltype').AsString);
CityIDList.Insert(i,InitQuery.FieldByName('CityID').AsString);
SiteidList.Insert(i,InitQuery.FieldByName('siteid').AsString);
i := i+1;
InitQuery.Next;
end;
maxcount := i;
except on E:Exceptiondo
showmessage('初始化失败!');
end;
end;
finally
InitQuery.Free;
end;
end;

{ TGetMM }
function TGetMM.Clear(source: string): string;
begin
--此处完成分析整理HTML
end;

constructor TGetMM.Create(ID,Url,CityID,UrlType,Siteid:TStringList;Sqlstr: string);
begin
FID:=ID;
FUrl:=Url;
FCityID :=CityID;
FUrlType :=UrlType;
FSiteid :=Siteid;
Fsqlstr :=Sqlstr;
FreeOnTerminate := true;
Fstrm:=TStringStream.Create('');
sleep(2000);
FIdGetMM:= TIdHTTP.Create(nil);
sleep(2000);
FADOQuery:= TADOQuery.Create(nil);
FADOQuery.ConnectionString :=ConnStr;
inherited Create(False);
end;

procedure TGetMM.Execute;
var
i : integer;
begin
CoInitialize(nil);
for i:=0 to FUrl.Count-1do
begin
try
FIdGetMM.Get(FUrl.Strings,Fstrm);
sleep(3000);
Fsource := Fstrm.DataString;
Fsource :=Clear(Fsource);
try
Fsqlstr :='insert into rentinfo(url,district,area,provider,telephone,floor,size,room,buildyear,towards,price) values(';
Fsqlstr :=Fsqlstr +''''+FUrl.Strings+''''+','+''''+Get(Fsource,DistrictList)+''''+','+''''+Get(Fsource,AreaList)+''''+','+''''+Get(Fsource,ProviderList)+''''+','+''''+Get(Fsource,TelephoneList)+''''+','+''''+Get(Fsource,FloorList)+''''+','+''''+Get(Fsource,SizeList)+''''+','+''''+Get(Fsource,RoomList)+''''+','+''''+Get(Fsource,YearList)+''''+','+''''+Get(Fsource,TowardsList)+''''+','+''''+Get(Fsource,PriceList)+''''+')';
FADOQuery.SQL.Clear;
FADOQuery.SQL.Text := Fsqlstr;
FADOQuery.ExecSQL;
sleep(500);
Fsqlstr :='update urlinfo set online=1 where id='+FID.Strings;
FADOQuery.SQL.Clear;
FADOQuery.SQL.Text := Fsqlstr;
FADOQuery.ExecSQL;
sleep(500);
except
Fsqlstr :='update urlinfo set online=3 where id='+FID.Strings;
FADOQuery.SQL.Clear;
FADOQuery.SQL.Text := Fsqlstr;
FADOQuery.ExecSQL;
end;
finally
//FADOQuery.Free;
//FIdGetMM.Free;
end;
end;
CoUninitialize;
end;

procedure TForm1.GetMMThread(ID,Url,CityID,UrlType,Siteid:TStringList;Sqlstr: string);
begin
RGet :=TGetMM.Create(ID,Url,CityID,UrlType,Siteid,Sqlstr);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
sleep(1000);
GetMMThread(IDList,UrlList,CityIDList,UrlTypeList,SiteidList,'');
end;

function TGetMM.Get(source: string;
TempList: TStringList): string;
begin
--提取信息
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
InitList();
if maxcount>0 then
Button3.Click;
end;
{
initialization
CoInitialize(nil);

finalization
CoUninitialize;}
end.
 
我的想法是从数据库里读出带下载的URL(URL已下载到数据库) 放在TSTRINGLIST
传入线程后依次下载,下载完成后线程自动释放
再用timer控件 搁一段时间(10秒)新建一个线程
现在的问题是CPU动不动就占用99% 而且下载速度也没提上来):
 
我怀疑是不是创建和释放线程占用cpu太多
解决方法: 就是创建一定数目的线程不释放,接收到URL则运行
但问题是TGetMM.Execute如何接收参数呢? 或者参数已消息形式发送(??该怎么弄呢?以前只看过调用系统API给窗口发消息,现在如何给线程发消息呢?而且还要指定线程(假设新建了10个线程,如何知道哪个线程没看运行呢??))
希望大哥大姐们帮帮小弟
 
哪位有好的例子可以发给我
EMAIL :onyliu@126.com 小弟先谢了!!!!!!
 
接受答案了.
 
后退
顶部