我准备写一个程序,定期从某网站上取数据,写入本地数据库。由于网站是PHP+Mysql,空间商不开放3306端口,所以我就写了一个接口文件,放在网站上,接口文件访问数据库,将数据库里的数据提取出来,以xml文件的形式返回,delphi就从接口文件中读取节点,然后写入数据库,我有9个表要更新,因此将每个表的更新写入了一个线程。在主窗体中一个表一个表地更新,我目前已经写了5个表的更新程序,竟然发现5个线程同时运行,我不需他们同时运行,而且我担心这样会不会拖垮网站服务器?如果何能让这几个进程排队运行,也就是表一更新完了,接着更新表二的。亦或者是我的思路出了问题?
以下是我的线程代码:
unit TMyTread;interfaceuses
Windows,StdCtrls,Classes,ADODB,SysUtils,Controls,ReadXML,XMLDoc,xmldom,XMLIntf,conn,Dialogs;type
MyTread = class(TThread)
private
GOODSLIST:TStringList;
UpdateType:string;
Get_Sql:string;
GOds_id,cat_ID,IMGID,PRICE,ID,Link_ID,BRAND:Integer;
MEMO:TMemo;
protected
procedure Execute; override;
procedure DownLoadGoods;//下载商品资料
procedure DownLoadGoods_cat;//下载商品扩展分类
procedure DownLoadGoods_gallery;//下载商品相册
procedure DownLoadMember_price;//下载会员价
procedure DownLoadLink_goods;//下载相关商品
public
constructor Create(SQLText:string;AUPDATETYPE:string;TT:TStringList;MM:TMemo);
end;implementation{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TMyTread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TMyTread }
constructor MyTread.Create(SQLText:string;AUPDATETYPE:string;TT:TStringList;MM:TMemo);
begin
inherited Create(False);
Get_Sql:=SQLText;
GOODSLIST:=TStringList.Create;//此处的stringlist变量由主窗体传入
GOODSLIST:=TT;
UpdateType:=AUPDATETYPE;//根据此变量确定更新哪个表
MEMO:=MM;
MEMO.Clear;
FreeOnTerminate:=True;
end;
procedure MyTread.Execute;//根据传入的AUPDATETYPE的值,来确定运行哪个过程。
var
i,j:Integer;
begin
i:=GOODSLIST.Count;
//ShowMessage(IntToStr(i));
if UpdateType='goods' then
begin
for j:=0 to i-1 do
begin
GOds_id:=StrToInt(GOODSLIST[j]); Synchronize(DownLoadGoods);
//Sleep(50);
end ;
end
else if UpdateType='goods_cat' then
begin
for j:=0 to i-1 do
begin
ID:=StrToInt(GOODSLIST[j]);
Synchronize(DownLoadGoods_cat);
end;
end
else if UpdateType='goods_gallery' then
begin
for j:=0 to i-1 do
begin
IMGID:=StrToInt(GOODSLIST[j]);
Synchronize(DownLoadGoods_gallery);
end;
end
else if UpdateType='member_price' then
begin
for j:=0 to i-1 do
begin
PRICE:=StrToInt(GOODSLIST[j]);
Synchronize(DownLoadMember_price);
end;
end
else if UpdateType='link_goods' then
begin
for j:=0 to i-1 do
begin
Link_ID:=StrToInt(GOODSLIST[j]);
Synchronize(DownLoadLink_goods);
end;
end;
if Terminated then
Exit;
end;//以下由于代码太长,所以略去将几个线程写入一个线程类里,如果要排队运行,难不成要建几个线程类?求大侠们指点迷津。
以下是我的线程代码:
unit TMyTread;interfaceuses
Windows,StdCtrls,Classes,ADODB,SysUtils,Controls,ReadXML,XMLDoc,xmldom,XMLIntf,conn,Dialogs;type
MyTread = class(TThread)
private
GOODSLIST:TStringList;
UpdateType:string;
Get_Sql:string;
GOds_id,cat_ID,IMGID,PRICE,ID,Link_ID,BRAND:Integer;
MEMO:TMemo;
protected
procedure Execute; override;
procedure DownLoadGoods;//下载商品资料
procedure DownLoadGoods_cat;//下载商品扩展分类
procedure DownLoadGoods_gallery;//下载商品相册
procedure DownLoadMember_price;//下载会员价
procedure DownLoadLink_goods;//下载相关商品
public
constructor Create(SQLText:string;AUPDATETYPE:string;TT:TStringList;MM:TMemo);
end;implementation{ Important: Methods and properties of objects in visual components can only be
used in a method called using Synchronize, for example, Synchronize(UpdateCaption); and UpdateCaption could look like, procedure TMyTread.UpdateCaption;
begin
Form1.Caption := 'Updated in a thread';
end; }{ TMyTread }
constructor MyTread.Create(SQLText:string;AUPDATETYPE:string;TT:TStringList;MM:TMemo);
begin
inherited Create(False);
Get_Sql:=SQLText;
GOODSLIST:=TStringList.Create;//此处的stringlist变量由主窗体传入
GOODSLIST:=TT;
UpdateType:=AUPDATETYPE;//根据此变量确定更新哪个表
MEMO:=MM;
MEMO.Clear;
FreeOnTerminate:=True;
end;
procedure MyTread.Execute;//根据传入的AUPDATETYPE的值,来确定运行哪个过程。
var
i,j:Integer;
begin
i:=GOODSLIST.Count;
//ShowMessage(IntToStr(i));
if UpdateType='goods' then
begin
for j:=0 to i-1 do
begin
GOds_id:=StrToInt(GOODSLIST[j]); Synchronize(DownLoadGoods);
//Sleep(50);
end ;
end
else if UpdateType='goods_cat' then
begin
for j:=0 to i-1 do
begin
ID:=StrToInt(GOODSLIST[j]);
Synchronize(DownLoadGoods_cat);
end;
end
else if UpdateType='goods_gallery' then
begin
for j:=0 to i-1 do
begin
IMGID:=StrToInt(GOODSLIST[j]);
Synchronize(DownLoadGoods_gallery);
end;
end
else if UpdateType='member_price' then
begin
for j:=0 to i-1 do
begin
PRICE:=StrToInt(GOODSLIST[j]);
Synchronize(DownLoadMember_price);
end;
end
else if UpdateType='link_goods' then
begin
for j:=0 to i-1 do
begin
Link_ID:=StrToInt(GOODSLIST[j]);
Synchronize(DownLoadLink_goods);
end;
end;
if Terminated then
Exit;
end;//以下由于代码太长,所以略去将几个线程写入一个线程类里,如果要排队运行,难不成要建几个线程类?求大侠们指点迷津。
解决方案 »
- 怎么检查一个文件是否有数字签名
- 超级菜鸟问题(送分)
- ADOQUERY提交SQL語句時出現的問題。
- 用TADOQuery执行“Insert into”错误!!!??
- 为什么会提示:incompatible type 'integer' and 'extended.
- 如何得到excel文件的某一单元格内容
- 心情不好还的工作,这样的问题用什么方法才不至于锁库?
- 300分求助,delphi+ADO+Oracle中的SQL语法错误!
- 把字符串写进文件,打开文件发现是乱码,请教一下怎么解决,很急,在线等待!
- 开发多层分布式数据库一定要有NT服务器吗?Win98单机能否实现?
- 请问如何改变LinkLabel控件超链接的颜色
- NSIS调用delphi的dll
第二,api的,用waitforsingleobject等待一个线程放回后,创建下一个线程
在主窗体调线程的代码如下:
MyTread.Create(FDM.con1.ConnectionString,'goods',SS1,mmo1);
MyTread.Create(FDM.con1.ConnectionString,'goods_cat',SS2,mmo1);
这两个线程如何让它们排队运行?