我写了一个线程,用一个Tstringlist变量传入参数,在线程里循环,但执行到31条后,就停止执行了,不知道为什么?
下面是代码
unit TMyTread;interfaceuses
Windows,StdCtrls,Classes,ADODB,SysUtils,Controls,ReadXML,XMLDoc,xmldom,XMLIntf,conn,Dialogs;type
MyTread = class(TThread)
private
GOODSLIST:TStringList;
Get_Sql:string;
MEMO:TMemo;
protected
procedure Execute; override;
procedure DownLoadGoods(GOODSID:Integer);
public
constructor Create(SQLText: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;TT:TStringList;MM:TMemo);
begin
inherited Create(False);
Get_Sql:=SQLText;
GOODSLIST:=TStringList.Create;
GOODSLIST:=TT;
MEMO:=MM;
MEMO.Clear;
FreeOnTerminate:=False;
end;
procedure MyTread.Execute;
var
i,j,GOdsID:Integer;
begin
i:=GOODSLIST.Count;
ShowMessage(IntToStr(i));//这里弹出683
for j:=0 to i-1 do
begin
GOdsID:=StrToInt(GOODSLIST[j]);//循环取出Stringlist里的值 DownLoadGoods(GOdsID);
//Sleep(50);
end ; if Terminated then
Exit;
end;
procedure Mytread.DownLoadGoods(GOODSID:Integer);
var
Query1,Query2:TADOQuery;
str,str2,sqltext:string;
XML:IXMLDocument;
NODE:IXMLNode;
i,j:Integer;
begin
Query1:=TADOQuery.Create(nil);
Query2:=TADOQuery.Create(nil);
// ShowMessage(str);
// XML:=TXMLDocument.Create(nil);
// XML.LoadFromFile(str);
// XML.Active:=True;
// NODE:=XML.DocumentElement;
// goods_id:=StrToInt(NODE.ChildNodes[j].NodeValue);
// ShowMessage(IntToStr(goods_id));
str2:='%s?uCode=%s&action=goodsInfo&goods_id=%d';
str2:=Format(str2,[zhuzhan_jiekou,JIERUMA,GOODSID]);
GoodsInfo:=get_goodsInfoList(GoodsInfo,str2);
with Query1 do
begin
ConnectionString:=Get_Sql;
SQL.Text:='select * from goods';
Open;
//与本地数据库对比,如果存丰goods_id相同的记录则修改,否则就插入 if not Locate('goods_id',GOODSID,[]) then
begin
sqltext:='insert into goods (goods_id,cat_id,goods_name,goods_sn,brand_id,goods_number,et_price,shop_price'
+',keywords,goods_brief,goods_desc,goods_thumb,goods_img,original_img,is_real,is_on_sale,is_alone_sale,integral,add_time,sort_order,is_best,is_new,is_hot,warn_number)'
+' values(:goods_id,:cat_id,:goods_name,:goods_sn,:brand_id,:goods_number,:et_price,:shop_price,:keywords,:goods_brief,:goods_desc,:goods_thumb,:goods_img,'
+':original_img,:is_real,:is_on_sale,:is_alone_sale,:integral,:add_time,:sort_order,:is_best,:is_new,:is_hot,:warn_number)';
end
else
begin
sqltext:=Format('update goods set goods_id=:goods_id,cat_id=:cat_id,goods_name=:goods_name,goods_sn=:goods_sn,brand_id=:brand_id,goods_number=:goods_number,et_price=:et_price,'
+'shop_price=:shop_price,keywords=:keywords,goods_brief=:goods_brief,goods_desc=:goods_desc,goods_thumb=:goods_thumb,goods_img=:goods_img,original_img=:original_img,is_real=:is_real'
+',is_on_sale=:is_on_sale,is_alone_sale=:is_alone_sale,integral=:integral,add_time=:add_time,sort_order=:sort_order,is_best=:is_best,is_new=:is_new,is_hot=:is_hot,warn_number=:warn_number where goods_id=%d',[goods_id]);
end;
with Query2 do
begin
ConnectionString:=Get_Sql;
SQL.Text:=sqltext;
Parameters.ParamByName('goods_id').Value:=GOODSID;
Parameters.ParamByName('goods_name').Value:=GoodsInfo.goods_name;
Parameters.ParamByName('cat_id').Value:=GoodsInfo.cat_id;
Parameters.ParamByName('goods_sn').Value:=GoodsInfo.goods_sn;
Parameters.ParamByName('brand_id').Value:=GoodsInfo.brand_id;
Parameters.ParamByName('goods_number').Value:=GoodsInfo.goods_number;
Parameters.ParamByName('et_price').Value:=GoodsInfo.et_price;
Parameters.ParamByName('shop_price').Value:=GoodsInfo.shop_price;
Parameters.ParamByName('keywords').Value:=GoodsInfo.keywords;
Parameters.ParamByName('goods_brief').Value:=GoodsInfo.goods_brief;
Parameters.ParamByName('goods_desc').Value:=GoodsInfo.goods_desc;
Parameters.ParamByName('goods_thumb').Value:=GoodsInfo.goods_thumb;
Parameters.ParamByName('goods_img').Value:=GoodsInfo.goods_img;
Parameters.ParamByName('original_img').Value:=GoodsInfo.original_img;
Parameters.ParamByName('is_real').Value:=GoodsInfo.is_real;
Parameters.ParamByName('is_on_sale').Value:=GoodsInfo.is_on_sale;
Parameters.ParamByName('is_alone_sale').Value:=GoodsInfo.is_alone_sale;
Parameters.ParamByName('integral').Value:=GoodsInfo.integral;
Parameters.ParamByName('add_time').Value:=FormatDateTime('YY-mm-dd',GoodsInfo.add_time);
Parameters.ParamByName('sort_order').Value:=GoodsInfo.sort_order;
Parameters.ParamByName('is_best').Value:=GoodsInfo.is_best;
Parameters.ParamByName('is_new').Value:=GoodsInfo.is_new;
Parameters.ParamByName('is_hot').Value:=GoodsInfo.is_hot;
Parameters.ParamByName('warn_number').Value:=GoodsInfo.warn_number;
ExecSQL; end;
end;
// Sleep(10);
MEMO.Lines.Add(GoodsInfo.goods_name+'下载成功!');
// Query1.Free;
//Query2.Free;
end;end.但是只执行了31条就停止了,不知道为什么,请高手相助!
下面是代码
unit TMyTread;interfaceuses
Windows,StdCtrls,Classes,ADODB,SysUtils,Controls,ReadXML,XMLDoc,xmldom,XMLIntf,conn,Dialogs;type
MyTread = class(TThread)
private
GOODSLIST:TStringList;
Get_Sql:string;
MEMO:TMemo;
protected
procedure Execute; override;
procedure DownLoadGoods(GOODSID:Integer);
public
constructor Create(SQLText: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;TT:TStringList;MM:TMemo);
begin
inherited Create(False);
Get_Sql:=SQLText;
GOODSLIST:=TStringList.Create;
GOODSLIST:=TT;
MEMO:=MM;
MEMO.Clear;
FreeOnTerminate:=False;
end;
procedure MyTread.Execute;
var
i,j,GOdsID:Integer;
begin
i:=GOODSLIST.Count;
ShowMessage(IntToStr(i));//这里弹出683
for j:=0 to i-1 do
begin
GOdsID:=StrToInt(GOODSLIST[j]);//循环取出Stringlist里的值 DownLoadGoods(GOdsID);
//Sleep(50);
end ; if Terminated then
Exit;
end;
procedure Mytread.DownLoadGoods(GOODSID:Integer);
var
Query1,Query2:TADOQuery;
str,str2,sqltext:string;
XML:IXMLDocument;
NODE:IXMLNode;
i,j:Integer;
begin
Query1:=TADOQuery.Create(nil);
Query2:=TADOQuery.Create(nil);
// ShowMessage(str);
// XML:=TXMLDocument.Create(nil);
// XML.LoadFromFile(str);
// XML.Active:=True;
// NODE:=XML.DocumentElement;
// goods_id:=StrToInt(NODE.ChildNodes[j].NodeValue);
// ShowMessage(IntToStr(goods_id));
str2:='%s?uCode=%s&action=goodsInfo&goods_id=%d';
str2:=Format(str2,[zhuzhan_jiekou,JIERUMA,GOODSID]);
GoodsInfo:=get_goodsInfoList(GoodsInfo,str2);
with Query1 do
begin
ConnectionString:=Get_Sql;
SQL.Text:='select * from goods';
Open;
//与本地数据库对比,如果存丰goods_id相同的记录则修改,否则就插入 if not Locate('goods_id',GOODSID,[]) then
begin
sqltext:='insert into goods (goods_id,cat_id,goods_name,goods_sn,brand_id,goods_number,et_price,shop_price'
+',keywords,goods_brief,goods_desc,goods_thumb,goods_img,original_img,is_real,is_on_sale,is_alone_sale,integral,add_time,sort_order,is_best,is_new,is_hot,warn_number)'
+' values(:goods_id,:cat_id,:goods_name,:goods_sn,:brand_id,:goods_number,:et_price,:shop_price,:keywords,:goods_brief,:goods_desc,:goods_thumb,:goods_img,'
+':original_img,:is_real,:is_on_sale,:is_alone_sale,:integral,:add_time,:sort_order,:is_best,:is_new,:is_hot,:warn_number)';
end
else
begin
sqltext:=Format('update goods set goods_id=:goods_id,cat_id=:cat_id,goods_name=:goods_name,goods_sn=:goods_sn,brand_id=:brand_id,goods_number=:goods_number,et_price=:et_price,'
+'shop_price=:shop_price,keywords=:keywords,goods_brief=:goods_brief,goods_desc=:goods_desc,goods_thumb=:goods_thumb,goods_img=:goods_img,original_img=:original_img,is_real=:is_real'
+',is_on_sale=:is_on_sale,is_alone_sale=:is_alone_sale,integral=:integral,add_time=:add_time,sort_order=:sort_order,is_best=:is_best,is_new=:is_new,is_hot=:is_hot,warn_number=:warn_number where goods_id=%d',[goods_id]);
end;
with Query2 do
begin
ConnectionString:=Get_Sql;
SQL.Text:=sqltext;
Parameters.ParamByName('goods_id').Value:=GOODSID;
Parameters.ParamByName('goods_name').Value:=GoodsInfo.goods_name;
Parameters.ParamByName('cat_id').Value:=GoodsInfo.cat_id;
Parameters.ParamByName('goods_sn').Value:=GoodsInfo.goods_sn;
Parameters.ParamByName('brand_id').Value:=GoodsInfo.brand_id;
Parameters.ParamByName('goods_number').Value:=GoodsInfo.goods_number;
Parameters.ParamByName('et_price').Value:=GoodsInfo.et_price;
Parameters.ParamByName('shop_price').Value:=GoodsInfo.shop_price;
Parameters.ParamByName('keywords').Value:=GoodsInfo.keywords;
Parameters.ParamByName('goods_brief').Value:=GoodsInfo.goods_brief;
Parameters.ParamByName('goods_desc').Value:=GoodsInfo.goods_desc;
Parameters.ParamByName('goods_thumb').Value:=GoodsInfo.goods_thumb;
Parameters.ParamByName('goods_img').Value:=GoodsInfo.goods_img;
Parameters.ParamByName('original_img').Value:=GoodsInfo.original_img;
Parameters.ParamByName('is_real').Value:=GoodsInfo.is_real;
Parameters.ParamByName('is_on_sale').Value:=GoodsInfo.is_on_sale;
Parameters.ParamByName('is_alone_sale').Value:=GoodsInfo.is_alone_sale;
Parameters.ParamByName('integral').Value:=GoodsInfo.integral;
Parameters.ParamByName('add_time').Value:=FormatDateTime('YY-mm-dd',GoodsInfo.add_time);
Parameters.ParamByName('sort_order').Value:=GoodsInfo.sort_order;
Parameters.ParamByName('is_best').Value:=GoodsInfo.is_best;
Parameters.ParamByName('is_new').Value:=GoodsInfo.is_new;
Parameters.ParamByName('is_hot').Value:=GoodsInfo.is_hot;
Parameters.ParamByName('warn_number').Value:=GoodsInfo.warn_number;
ExecSQL; end;
end;
// Sleep(10);
MEMO.Lines.Add(GoodsInfo.goods_name+'下载成功!');
// Query1.Free;
//Query2.Free;
end;end.但是只执行了31条就停止了,不知道为什么,请高手相助!
解决方案 »
- Listview下标越界如何解决
- 文件备份和恢复
- DBGrid显示数据库中多表问题
- post数据不成功时,怎么知道到底是什么原因造成的不成功?
- 如何 实现?
- 求购《2004应届大学毕业生就业信息Career Information》上海市高校毕业生就业指导中心编写
- statusbar的问题
- 现在delphi只支持6种数据库,如果我想用access做,如何做?
- sql和delphi的问题,令人头痛!!!
- 我用的ADOTable控件,Active初始为true,若为空表时,关闭程序总会出现异常对话框!请问在Active不初始为false的情况下,如何处理!
- 窗口无边框后,任务栏没右键菜单了
- access查询时间段问题:怎样包括两头时间?
MEMO.Lines.Add(GoodsInfo.goods_name+'下载成功!'); 这句最好加临界区访问临界区
2.打断点,到31条的时候,看一下数据库操作相关的部分是否正确,就是看看SQL有关的部分有没有正确执行~
第一,这个线程有时候不反应,必须点击2次甚至几次,才会有反应(Memo控件里会出现字)
第二,线程执行完毕后,退出时会报错。
(我晕,到现在我才知道不能上传截图)
system erroer code:1400,无效的窗口句柄;
runtime error 217 at 0040F3A6