我在一个单线程理用了个ADOQuery,向sql数据库插入很多数据,现在有2个问题(50分)

  • 主题发起人 主题发起人 mazheng
  • 开始时间 开始时间
M

mazheng

Unregistered / Unconfirmed
GUEST, unregistred user!
我在一个单线程理用了个ADOQuery,向sql数据库插入很多数据,现在有2个问题 ,当用户点击‘停止’按钮时,如何在停止线程的同时 停止数据的插入,2,为什么程序会自动退出??
 
到底哪里出问题了???<br><br>ype<br>&nbsp; Txyz = class(TThread)<br>&nbsp; published<br>&nbsp; &nbsp; procedure Execute; override;<br>&nbsp; &nbsp; procedure displaymemo;<br>&nbsp; end;<br><br>var<br>&nbsp; ClientMainFrm: TClientMainFrm;<br>&nbsp; XYZThread: Txyz;<br><br>&nbsp; Y_Table,B_Table,SqlString:string;<br>&nbsp; myCursor:tCursor;<br>&nbsp; myint,MyWay,Sint,Oint,LastTimeSid,ThisTimeSid,LastTimeOid,ThisTimeOid:Integer;<br>&nbsp; sl:TStringList;<br><br>implementation<br>//truncate table msg<br>{$R *.dfm}<br>//==============================================================================<br>procedure Txyz.displaymemo;<br>var LocalSourceid,RemoteSourceId:Integer;<br>begin<br>&nbsp; //Sbool:=False;Obool:=False;<br>&nbsp; ClientMainFrm.stat1.Panels.Items[0].Text:='正在同步...';<br>&nbsp; ClientMainFrm.mmoRunTxt.Lines.Add('正在同步...');<br>&nbsp; ClientMainFrm.mmoRunTxt.Clear;<br>&nbsp; myCursor:=ClientMainFrm.Cursor;<br>&nbsp; ClientMainFrm.Cursor:=crHourGlass;<br>&nbsp; ClientMainFrm.tmr1.Enabled:=False;<br>&nbsp; ClientMainFrm.btnSet.Enabled:=False;<br><br>&nbsp; RemoteSourceId := ClientMainFrm.myini.ReadInteger('Source','远程source',0);<br>&nbsp; LocalSourceid &nbsp;:= ClientMainFrm.myini.ReadInteger('Source','本地source',0);<br>&nbsp; Coinitialize(nil);<br>&nbsp; case MyWay of<br>&nbsp; &nbsp; 0:ClientMainFrm.InstRemote(RemoteSourceId,LocalSourceid); //同步到远程<br>&nbsp; &nbsp; 1:ClientMainFrm.InstLocal(RemoteSourceId,LocalSourceid); &nbsp;//同步到本地<br>&nbsp; &nbsp; 2:begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ClientMainFrm.mmoRunTxt.Lines.add('双向同步:');<br>&nbsp; &nbsp; &nbsp; &nbsp; ClientMainFrm.mmoRunTxt.Lines.add('');<br>&nbsp; &nbsp; &nbsp; &nbsp; ClientMainFrm.InstLocal(RemoteSourceId,LocalSourceid);//先执行'同步到本地‘<br>&nbsp; &nbsp; &nbsp; &nbsp; ClientMainFrm.InstRemote(RemoteSourceId,LocalSourceid);//先执行'同步到远程<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; CoUnInitialize;<br><br>end;<br>//==============================================================================<br>procedure Txyz.Execute;<br>begin<br>&nbsp; while not Terminated do<br>&nbsp; begin<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; displaymemo;<br>&nbsp; &nbsp; &nbsp; ClientMainFrm.ThreadFree;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; sl.Add(DateTimeToStr(Now)+'--线程错误');<br>&nbsp; &nbsp; &nbsp; sl.SaveToFile('c:/测试.txt');<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.InstLocal(const ARemoteSourceId,ALocalSourceid:integer);<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; ThisTimeOid:=TopConst(con1,y_Table);<br>&nbsp; &nbsp; Oint:= ThisTimeOid - LastTimeOid;<br>&nbsp; &nbsp; LastTimeOid:=ThistimeOid;<br>&nbsp; &nbsp; if Oint&gt;0 then<br>&nbsp; &nbsp; &nbsp; if OpenLongDistanceData(y_Table,Oint) then<br>&nbsp; &nbsp; &nbsp; &nbsp; InPhaseUpdate(qry1,qry2,b_Table,'同步到本地:',ARemoteSourceId,ALocalSourceid,True);<br>&nbsp; except<br>&nbsp; &nbsp; sl.Add(DateTimeToStr(Now)+'--InstRemote错误');<br>&nbsp; &nbsp; sl.SaveToFile('c:/测试.txt');<br>&nbsp; end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.InstRemote(const ARemoteSourceId,ALocalSourceid:integer);<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; ThisTimeSid:=TopConst(con2,B_Table);<br>&nbsp; &nbsp; sint:= ThisTimeSid - LastTimeSid;<br>&nbsp; &nbsp; LastTimeSid:=ThisTimeSid;<br>&nbsp; &nbsp; if Sint&gt;0 then<br>&nbsp; &nbsp; &nbsp; if OpenData(B_Table,Sint) then<br>&nbsp; &nbsp; &nbsp; &nbsp; InPhaseUpdate(qry2,qry1,Y_Table,'同步到远程:',ALocalSourceid,ARemoteSourceId,False);<br>&nbsp; except<br>&nbsp; &nbsp; sl.Add(DateTimeToStr(Now)+'--InstLocal错误');<br>&nbsp; &nbsp; sl.SaveToFile('c:/测试.txt');<br>&nbsp; end;<br>end;<br>//==============================================================================<br>function TClientMainFrm.TopConst(Acon:TADOConnection;ATable:string):Integer;<br>var QryTmp:TADOQuery;<br>begin<br>&nbsp; qrytmp:=TADOQuery.Create(nil);<br>&nbsp; try<br>&nbsp; &nbsp; qrytmp.Connection:=Acon;<br>&nbsp; &nbsp; qrytmp.Close;<br>&nbsp; &nbsp; qrytmp.SQL.Clear;<br>&nbsp; &nbsp; qrytmp.SQL.Add('select top 1 id from '+ATable+' order by id desc');<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; QryTmp.Open;<br>&nbsp; &nbsp; &nbsp; Result:=StrToInt(QryTmp.Fields[0].Text);<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; Result:=0;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; qrytmp.Close;<br>&nbsp; &nbsp; QryTmp.Free;<br>&nbsp; end;<br>end;<br>//==============================================================================<br>function TClientMainFrm.RunSql(Aqry:TADOQuery;Atext:string;i:Integer; Abool:Boolean):Boolean;<br>var t:string;<br>begin<br>&nbsp; if Abool then<br>&nbsp; begin<br>&nbsp; &nbsp; With AQry Do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; &nbsp; SQL.Clear ;<br>&nbsp; &nbsp; &nbsp; SQL.Add('EXECUTE insttext :s1, :s2, :s3, :s4, :s5 Output ') ;<br>&nbsp; &nbsp; &nbsp; Parameters.Items[0].Value := strFields1;<br>&nbsp; &nbsp; &nbsp; Parameters.Items[1].Value := strFields2;<br>&nbsp; &nbsp; &nbsp; Parameters.Items[2].Value := strFields3;<br>&nbsp; &nbsp; &nbsp; Parameters.Items[3].Value := strFields4;<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; Prepared;<br>&nbsp; &nbsp; &nbsp; &nbsp; ExecSQL;<br>&nbsp; &nbsp; &nbsp; &nbsp; T := Parameters.Items[4].Value;<br>&nbsp; &nbsp; &nbsp; &nbsp; if t='0' then Result:=True<br>&nbsp; &nbsp; &nbsp; &nbsp; else Result:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; Result:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end else<br>&nbsp; begin<br>&nbsp; &nbsp; With AQry Do<br>&nbsp; &nbsp; Begin<br>&nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; &nbsp; SQL.Clear ;<br>&nbsp; &nbsp; &nbsp; SQL.Add(Atext);<br>&nbsp; &nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp; if i=1 then Open<br>&nbsp; &nbsp; &nbsp; &nbsp; else ExecSQL;<br>&nbsp; &nbsp; &nbsp; &nbsp; Result:=True<br>&nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; Result:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; Close;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.ThreadFree;<br>begin<br><br>&nbsp; stat1.Panels.Items[0].Text:='';<br>&nbsp; Cursor:=myCursor;<br>&nbsp; tmr1.Enabled:=True;<br>&nbsp; pb1.Position:=0;<br>&nbsp; Sqltxt:='';<br>&nbsp; strFields1:='';<br>&nbsp; strFields2:='';<br>&nbsp; strFields3:='';<br>&nbsp; strFields4:='';<br>&nbsp; Threadbool:=False;<br>&nbsp; XYZThread.Terminate;<br>&nbsp; qry1.Close;<br>&nbsp; qry2.Close;<br>&nbsp; XYZThread.WaitFor;<br>&nbsp; XYZThread.Free;<br>&nbsp; XYZThread:=nil;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.InPhaseUpdate(ASqry,AOqry:TADOQuery;ATable,AstrTxt:string;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ASSource,AOSource:Integer;Abool:Boolean);<br>var mSource,nSource,Stmp:string;i:Integer;<br>begin<br>&nbsp; mmoRunTxt.Lines.Add('');<br>&nbsp; mmoRunTxt.Lines.Add(AstrTxt);<br>&nbsp; mmoRunTxt.Lines.Add('');<br><br>&nbsp; mSource:=inttostr(ASSource);<br>&nbsp; pb1.Position:=0;<br>&nbsp; pb1.Max:=ASqry.RecordCount-1;<br>&nbsp; ASqry.Last;<br>&nbsp; while not ASqry.Bof do<br>&nbsp; begin<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; pb1.Position:=pb1.Position+1;<br>&nbsp; &nbsp; &nbsp; nSource:=Trim(Asqry.Fields.Fields[4].Text);<br>&nbsp; &nbsp; &nbsp; if nSource &lt;&gt; mSource then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; strFields1:=Asqry.Fields.Fields[1].Text;<br>&nbsp; &nbsp; &nbsp; &nbsp; strFields2:=Asqry.Fields.Fields[2].Text;<br>&nbsp; &nbsp; &nbsp; &nbsp; strFields3:=Asqry.Fields.Fields[3].Text;<br>&nbsp; &nbsp; &nbsp; &nbsp; strFields4:=inttostr(AOSource);<br>&nbsp; &nbsp; &nbsp; &nbsp; //------------------------------<br>&nbsp; &nbsp; &nbsp; &nbsp; Sqltxt:='INSERT INTO ' + ATable +' (msg,tel,code,source) VALUES (';<br>&nbsp; &nbsp; &nbsp; &nbsp; Sqltxt:=Sqltxt+'N'''+Asqry.Fields.Fields[1].Text+''',';<br>&nbsp; &nbsp; &nbsp; &nbsp; Sqltxt:=Sqltxt+'N'''+ASqry.Fields.Fields[2].Text+''',';<br>&nbsp; &nbsp; &nbsp; &nbsp; Sqltxt:=Sqltxt+'N'''+ASqry.Fields.Fields[3].Text+''',';<br>&nbsp; &nbsp; &nbsp; &nbsp; Sqltxt:=Sqltxt+'N'''+inttostr(AOSource)+''')';<br>&nbsp; &nbsp; &nbsp; &nbsp; //------------------------------<br>&nbsp; &nbsp; &nbsp; &nbsp; Stmp:=' 插入记录:[ (源库ID:'+Asqry.Fields.Fields[0].Text+') ';<br>&nbsp; &nbsp; &nbsp; &nbsp; Stmp:=Stmp+Asqry.Fields.Fields[1].Text+',';<br>&nbsp; &nbsp; &nbsp; &nbsp; Stmp:=Stmp+Asqry.Fields.Fields[2].Text+',';<br>&nbsp; &nbsp; &nbsp; &nbsp; Stmp:=Stmp+Asqry.Fields.Fields[3].Text+',';<br>&nbsp; &nbsp; &nbsp; &nbsp; Stmp:=Stmp+inttostr(AOSource)+']';<br>&nbsp; &nbsp; &nbsp; &nbsp; //-------------------------------<br>&nbsp; &nbsp; &nbsp; &nbsp; if RunSql(AOqry,Sqltxt,0,Abool) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mmoRunTxt.Lines.Add(Stmp);<br>&nbsp; &nbsp; &nbsp; &nbsp; //-------------------------------<br>&nbsp; &nbsp; &nbsp; &nbsp; for i:=1 to 1000 do ;<br>&nbsp; &nbsp; &nbsp; &nbsp; //Application.ProcessMessages;<br>&nbsp; &nbsp; &nbsp; &nbsp; strFields1:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; strFields2:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; strFields3:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; strFields4:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; Sqltxt:='';<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; ASqry.Prior;<br>&nbsp; end;<br>&nbsp; ASqry.Close;<br>&nbsp; //MsgBox('ok');<br>&nbsp; pb1.Position:=0;<br>end;<br>//==============================================================================<br>function TClientMainFrm.OpenLongDistanceData(ATable:string;ATopVal:Integer):Boolean;<br>begin &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //id,msgtime,<br>&nbsp; Sqltxt:='SELECT top '+inttostr(ATopVal)+'id,msg,tel,code,source FROM '+ATable;<br>&nbsp; Sqltxt:=Sqltxt + ' order by id desc';<br><br>&nbsp; if not RunSql(qry1,Sqltxt,1,False) then<br>&nbsp; begin<br>&nbsp; &nbsp; mmoRunTxt.Lines.Add('远程数据表打开失败。');<br>&nbsp; &nbsp; cltrycnTrayIcon.Hint:='数据同步器';<br>&nbsp; &nbsp; Result:=False;<br>&nbsp; end else<br>&nbsp; &nbsp; Result:=True;<br>end;<br>//==============================================================================<br>function TClientMainFrm.OpenData(ATable:string;ATopVal:Integer):Boolean;<br>begin<br>&nbsp; Sqltxt:='SELECT top '+inttostr(ATopVal)+'id,msg,tel,code,source FROM '+ATable;<br>&nbsp; Sqltxt:=Sqltxt + ' order by id desc';<br><br>&nbsp; if not RunSql(qry2,Sqltxt,1,False) then<br>&nbsp; begin<br>&nbsp; &nbsp; mmoRunTxt.Lines.Add('本地数据表打开失败。');<br>&nbsp; &nbsp; cltrycnTrayIcon.Hint:='数据同步器';<br>&nbsp; &nbsp; Result:=False;<br>&nbsp; end else<br>&nbsp; &nbsp; Result:=True;<br>end;<br>//==============================================================================<br>function TClientMainFrm.LinkServer:Boolean;<br>begin<br>&nbsp; Result:=False;<br>&nbsp; if FileExists(StrUdl) then<br>&nbsp; begin<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; con2.Close;<br>&nbsp; &nbsp; &nbsp; con2.ConnectionString:='FILE NAME='+StrUdl;<br>&nbsp; &nbsp; &nbsp; con2.Open;<br>&nbsp; &nbsp; &nbsp; Result:=True;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; mmoRunTxt.Lines.Add('本地数据库链接失败。');<br>&nbsp; &nbsp; &nbsp; cltrycnTrayIcon.Hint:='数据同步器';<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>//==============================================================================<br>function TClientMainFrm.LinkLongDistanceServer:Boolean;<br>var sqlip,sqlUser,sqlkey,sqlName:string;<br>begin<br>&nbsp; sqlip &nbsp; &nbsp; := Trim(myini.ReadString('SET','SqlIp',''));<br>&nbsp; sqlName &nbsp; := Trim(myini.ReadString('SET','SqlName',''));<br>&nbsp; sqlUser &nbsp; := Trim(myini.ReadString('SET','SqlUser',''));<br>&nbsp; sqlkey &nbsp; &nbsp;:= Trim(myini.ReadString('SET','Sqlkey',''));<br>&nbsp; Y_Table &nbsp; := Trim(myini.ReadString('TABLE','远程表',''));<br>&nbsp; B_Table &nbsp; := Trim(myini.ReadString('TABLE','本地表',''));<br><br>&nbsp; SqlString := 'Provider=SQLOLEDB.1;Password='+sqlkey +';Persist Security Info=True;';<br>&nbsp; SqlString := SqlString+'User ID='+sqlUser+';';<br>&nbsp; SqlString := SqlString+'Initial Catalog='+sqlName+';';<br>&nbsp; SqlString := SqlString+'Data Source='+ sqlip;<br>&nbsp; try<br>&nbsp; &nbsp; con1.Close;<br>&nbsp; &nbsp; con1.ConnectionString:=SqlString;<br>&nbsp; &nbsp; con1.Open;<br>&nbsp; &nbsp; Result := True;<br>&nbsp; except<br>&nbsp; &nbsp; mmoRunTxt.Lines.Add('远程数据库链接失败。');<br>&nbsp; &nbsp; cltrycnTrayIcon.Hint:='数据同步器';<br>&nbsp; &nbsp; Result:=False;<br>&nbsp; end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.SetFrmShow;<br>var SetFrm: TSetFrm;<br>begin<br>&nbsp; tmr1.Enabled:=False;<br>&nbsp; SetFrm:= TSetFrm.Create(nil);<br>&nbsp; try<br>&nbsp; &nbsp; SetFrm.ShowModal;<br>&nbsp; &nbsp; if SetFrm.SetBool then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; LastTimeSid:=0;<br>&nbsp; &nbsp; &nbsp; LastTimeOid:=0;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; SetFrm.Free;<br>&nbsp; end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.FormCreate(Sender: TObject);<br>var mbool:Boolean;<br>begin<br>// &nbsp;mbool:=False;<br>&nbsp; Myint &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= 0;<br>&nbsp; Sint &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := 0;<br>&nbsp; oint &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := 0;<br>&nbsp; LastTimeSid &nbsp; &nbsp;:= 0;<br>&nbsp; LastTimeOid &nbsp; &nbsp;:= 0;<br>&nbsp; LocalSourceid &nbsp;:= 0;<br>&nbsp; RemoteSourceId := 0;<br>&nbsp; strFields1 &nbsp; &nbsp; := '';<br>&nbsp; strFields2 &nbsp; &nbsp; := '';<br>&nbsp; strFields3 &nbsp; &nbsp; := '';<br>&nbsp; strFields4 &nbsp; &nbsp; := '';<br>&nbsp; Sqltxt &nbsp; &nbsp; &nbsp; &nbsp; := '';<br>&nbsp; Threadbool &nbsp; &nbsp; := False;<br>&nbsp; sl:=TStringList.Create;;<br>&nbsp; sl.Clear;<br>&nbsp; //--------------------------------<br>&nbsp; StrUdl:= ExtractFilePath(Application.ExeName)+'nclient.udl';<br>&nbsp; inifiles:= ExtractFilePath(Application.ExeName)+'NClient.ini';<br>&nbsp; myini:=tinifile.Create(inifiles);<br>&nbsp; if not FileExists(StrUdl) then<br>&nbsp; begin<br>&nbsp; &nbsp; ErrBox('文件[nclient.udl]丢失,程序无法启动');<br>&nbsp; &nbsp; Application.Terminate;<br>&nbsp; end;<br>&nbsp; if not FileExists(inifiles) then SetFrmShow;<br>&nbsp; MyInterval &nbsp; &nbsp; := myini.ReadInteger('TimeWay','time',0)*1000;<br>&nbsp; MyWay &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= myini.ReadInteger('TimeWay','Way',0);<br>&nbsp; tmr1.Interval &nbsp;:= MyInterval;<br>&nbsp; mbool:=myini.ReadBool('Automatic','auto',False);<br>&nbsp; if mbool then<br>&nbsp; &nbsp; btnStart.Click;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.btnExtClick(Sender: TObject);<br>begin<br>&nbsp; Close;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.btnSetClick(Sender: TObject);<br>begin<br>&nbsp; SetFrmShow;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.tmr1Timer(Sender: TObject);<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; Threadbool:=True;<br>&nbsp; &nbsp; XYZThread := Txyz.Create(False);<br>&nbsp; except<br>&nbsp; &nbsp; sl.Add(DateTimeToStr(Now)+'--创建线程时错误');<br>&nbsp; &nbsp; sl.SaveToFile('c:/测试.txt');<br>&nbsp; end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.N1Click(Sender: TObject);<br>begin<br>&nbsp; cltrycnTrayIcon.ShowMainForm;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.btnStartClick(Sender: TObject);<br>begin<br>&nbsp; btnStart.Enabled:=False;<br>&nbsp; try<br>&nbsp; &nbsp; if Myint=0 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if ClientMainFrm.LinkLongDistanceServer and ClientMainFrm.LinkServer then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; myint:=1;<br>&nbsp; &nbsp; &nbsp; &nbsp; stat1.Panels.Items[0].Text:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; mmoRunTxt.Lines.Clear;<br>&nbsp; &nbsp; &nbsp; &nbsp; //----------------------------------//<br>&nbsp; &nbsp; &nbsp; &nbsp; ThisTimeSid:=TopConst(con2,B_Table);<br>&nbsp; &nbsp; &nbsp; &nbsp; LastTimeSid:=ThisTimeSid;<br>&nbsp; &nbsp; &nbsp; &nbsp; ThisTimeOid:=TopConst(con1,y_Table);<br>&nbsp; &nbsp; &nbsp; &nbsp; LastTimeOid:=ThisTimeOid;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if myint=1 then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if btnStart.Caption='开始' then<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; cltrycnTrayIcon.Hint:='数据同步器-正在同步';<br>&nbsp; &nbsp; &nbsp; &nbsp; btnStart.Caption:='暂停';<br>&nbsp; &nbsp; &nbsp; &nbsp; //stat1.Panels.Items[0].Text:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; btnSet.Enabled:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; tmr1.Enabled:=True;<br>&nbsp; &nbsp; &nbsp; end else<br>&nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; if btnStart.Caption='暂停' then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Threadbool then XYZThread.Suspend<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else tmr1.Enabled:=False;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stat1.Panels.Items[0].Text:='暂停...';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; btnStart.Caption:='继续';<br>&nbsp; &nbsp; &nbsp; &nbsp; end else<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Threadbool then XYZThread.Resume<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else tmr1.Enabled:=True;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; btnStart.Caption:='暂停';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stat1.Panels.Items[0].Text:='';<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; btnStart.Enabled:=True;<br>&nbsp; end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.btn3Click(Sender: TObject);<br>begin<br>&nbsp; if Threadbool then ThreadFree;<br>&nbsp; cltrycnTrayIcon.Hint:='数据同步器';<br>&nbsp; stat1.Panels.Items[0].Text:='';<br>&nbsp; if tmr1.Enabled then tmr1.Enabled:=False;<br>&nbsp; if btnStart.Caption&lt;&gt;'开始' then btnStart.Caption:='开始';<br>&nbsp; btnSet.Enabled:=True;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.FormCloseQuery(Sender: TObject;<br>&nbsp; var CanClose: Boolean);<br>begin<br>&nbsp; if not YesNoBox('真的要退出?') then<br>&nbsp; &nbsp; CanClose := False<br>&nbsp; else begin<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; btn3.Click;<br>&nbsp; &nbsp; &nbsp; myini.Free;<br>&nbsp; &nbsp; &nbsp; sl.Free;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>//==============================================================================<br>end.
 
你的代码不要贴这么多了,没有人能看完,你把你的程序框架写一下,把主要的框架留下就是了。
 
没空看代码,不知道楼主有否注意ADOQuery的线程不安全性,线程是个死循环,线程程序<br>自己退出,一般是程序有问题,比如资源,内存和其它冲突。
 
就是每隔几秒创建一个单线程 ,在线程里执行对数据库的查询、插入 操作! 现在有2个问题 ,当用户点击‘停止’按钮时,如何在停止线程的同时 停止数据的插入,2,为什么程序会自动退出?? &nbsp;<br>//==============================================================================<br>procedure TClientMainFrm.tmr1Timer(Sender: TObject);<br>begin<br>&nbsp; try<br>&nbsp; &nbsp; Threadbool:=True;<br>&nbsp; &nbsp; XYZThread := Txyz.Create(False);<br>&nbsp; except<br>&nbsp; &nbsp; sl.Add(DateTimeToStr(Now)+'--创建线程时错误');<br>&nbsp; &nbsp; sl.SaveToFile('c:/测试.txt');<br>&nbsp; end;<br>end;<br><br>//==============================================================================<br>procedure Txyz.displaymemo;<br>var LocalSourceid,RemoteSourceId:Integer;<br>begin<br>&nbsp; //Sbool:=False;Obool:=False;<br>&nbsp; ClientMainFrm.stat1.Panels.Items[0].Text:='正在同步...';<br>&nbsp; ClientMainFrm.mmoRunTxt.Lines.Add('正在同步...');<br>&nbsp; ClientMainFrm.mmoRunTxt.Clear;<br>&nbsp; myCursor:=ClientMainFrm.Cursor;<br>&nbsp; ClientMainFrm.Cursor:=crHourGlass;<br>&nbsp; ClientMainFrm.tmr1.Enabled:=False;<br>&nbsp; ClientMainFrm.btnSet.Enabled:=False;<br><br>&nbsp; RemoteSourceId := ClientMainFrm.myini.ReadInteger('Source','远程source',0);<br>&nbsp; LocalSourceid &nbsp;:= ClientMainFrm.myini.ReadInteger('Source','本地source',0);<br>&nbsp; Coinitialize(nil);<br>&nbsp; case MyWay of<br>&nbsp; &nbsp; 0:ClientMainFrm.InstRemote(RemoteSourceId,LocalSourceid); //同步到远程<br>&nbsp; &nbsp; 1:ClientMainFrm.InstLocal(RemoteSourceId,LocalSourceid); &nbsp;//同步到本地<br>&nbsp; &nbsp; 2:begin<br>&nbsp; &nbsp; &nbsp; &nbsp; ClientMainFrm.mmoRunTxt.Lines.add('双向同步:');<br>&nbsp; &nbsp; &nbsp; &nbsp; ClientMainFrm.mmoRunTxt.Lines.add('');<br>&nbsp; &nbsp; &nbsp; &nbsp; ClientMainFrm.InstLocal(RemoteSourceId,LocalSourceid);//先执行'同步到本地‘<br>&nbsp; &nbsp; &nbsp; &nbsp; ClientMainFrm.InstRemote(RemoteSourceId,LocalSourceid);//先执行'同步到远程<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; CoUnInitialize;<br><br>end;<br>//==============================================================================<br>procedure Txyz.Execute;<br>begin<br>&nbsp; while not Terminated do<br>&nbsp; begin<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; displaymemo;<br>&nbsp; &nbsp; &nbsp; ClientMainFrm.ThreadFree;<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; sl.Add(DateTimeToStr(Now)+'--线程错误');<br>&nbsp; &nbsp; &nbsp; sl.SaveToFile('c:/测试.txt');<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.ThreadFree;<br>begin<br><br>&nbsp; stat1.Panels.Items[0].Text:='';<br>&nbsp; Cursor:=myCursor;<br>&nbsp; tmr1.Enabled:=True;<br>&nbsp; pb1.Position:=0;<br>&nbsp; Sqltxt:='';<br>&nbsp; strFields1:='';<br>&nbsp; strFields2:='';<br>&nbsp; strFields3:='';<br>&nbsp; strFields4:='';<br>&nbsp; Threadbool:=False;<br>&nbsp; XYZThread.Terminate;<br>&nbsp; qry1.Close;<br>&nbsp; qry2.Close;<br>&nbsp; XYZThread.WaitFor;<br>&nbsp; XYZThread.Free;<br>&nbsp; XYZThread:=nil;<br>end;<br>//==============================================================================<br>procedure TClientMainFrm.btn3Click(Sender: TObject);//停止按钮<br>begin<br>&nbsp; if Threadbool then ThreadFree;<br>&nbsp; cltrycnTrayIcon.Hint:='数据同步器';<br>&nbsp; stat1.Panels.Items[0].Text:='';<br>&nbsp; if tmr1.Enabled then tmr1.Enabled:=False;<br>&nbsp; if btnStart.Caption&lt;&gt;'开始' then btnStart.Caption:='开始';<br>&nbsp; btnSet.Enabled:=True;<br>end;<br>//==============================================================================
 
给你一个思路:<br>1、定义一个全局变量StopRun,表示是否停止执行;<br>2、线程创建前StopRun = false(不停止执行);<br>3、线程执行过程中检测StopRun是否为true,如果被改为true则线程自己终止运行;<br>4、不要试图从外部终止线程;<br>5、此方法只能处理一个线程,否则多个现成将同时终止;
 
后退
顶部