我写了一个线程,用一个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条就停止了,不知道为什么,请高手相助!

解决方案 »

  1.   

    你的线程里有访问Memo,必须要同步啊。不知道是不是这个原因引起的。
      

  2.   

    不知道你的GOODSLIST是从哪得来的,你在线程的for循环时,不要再对GOODSLIST进行修改,否则就会出问题另外操作主进程vcl,注意同步
      

  3.   

    +++
    MEMO.Lines.Add(GoodsInfo.goods_name+'下载成功!'); 这句最好加临界区访问临界区
      

  4.   

    1.线程里直接访问vcl组件了,要用同步;
    2.打断点,到31条的时候,看一下数据库操作相关的部分是否正确,就是看看SQL有关的部分有没有正确执行~
      

  5.   

    谢谢大家的指点,这个问题解决了,我只在DownLoadGoods过程里等SQL语句执行完毕后,再加上一句sleep(50);问题就解决了。但现在有个新问题。
    第一,这个线程有时候不反应,必须点击2次甚至几次,才会有反应(Memo控件里会出现字)
    第二,线程执行完毕后,退出时会报错。
    (我晕,到现在我才知道不能上传截图)
    system erroer code:1400,无效的窗口句柄;
    runtime error 217 at 0040F3A6