请问如何在线程运结束时,退出程序(20分)

  • 主题发起人 主题发起人 shydbbs
  • 开始时间 开始时间
S

shydbbs

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,我的线程代码:
Constructor WzClThread.Create(Suspended:Boolean);
begin
inherited Create(Suspended);
datam:=tdm.Create(nil);
FreeOnTerminate:=True;
end;

{
Destructor WzClThread.Destroy;
begin
if Assigned(datam) then
begin
FreeAndNil(datam);
Application.Terminate;
end;
end;
}

Procedure WzClThread.ExecSql(var SqlText:String);
begin
try
with datamdo
begin
q.Connection:=Writecon;
q.close;
q.sql.clear;
q.sql.add(Sqltext);
q.ExecSQL;
end;
except
end;
end;

procedure WzClThread.Execute;
Var
ConIni:TiniFile;
OraPro,OraPwd,OraUID,OraDBSou:String;
RUid,RDaSou,WDaSou:String;
Cp,Cxdh,Wz,Sql1,Sql2,sql3:String;
// 车牌号,车型代号,违章行为
sCfd,sName,sSfz,sJsz:string;
//处罚单,姓名,身份证号,驾驶证号
sWzDate,sWzAdd,sWzCode,sKf,sFk:string;
begin
{ Place thread code here }
Form1.Label1.Visible:=true;
Form1.Label1.Caption:='正在处理数据,请稍等...';
try
ActiveX.CoInitialize(nil);
ConIni:=TiniFile.Create(ExtractFilePath(Application.ExeName)+'DbConnect.ini');
Orapro := ConIni.ReadString('OraCon','Provider','');
OraPwd := ConIni.readString('OraCon','Password','');
OraUID := ConIni.readString('Oracon','User ID','');
OraDBSou := ConIni.readString('OraCon','Data Source','');
RUid:=conIni.ReadString('Rcon','User ID','');
RDaSou:=conIni.ReadString('Rcon','Data Source','');
datam.OraDBCon.Close;
datam.OraDBCon.connectionString:=
Format('Provider=%s;Password=%s;User ID=%s;Data Source=%s',
[OraPro,OraPwd,OraUID,OraDBSou]);
datam.ReadCon.Close;
datam.ReadCon.ConnectionString:=
'Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=admin;Data Source='
+RDaSou+';Persist Security Info=False;Jet OLEDB:Database Password=""';
WDaSou:=ConIni.ReadString('Wcon','Data Source','');
datam.WriteCon.Close;
datam.WriteCon.ConnectionString:='Provider=Microsoft.Jet.OLEDB.4.0;Password="";User ID=admin;Data Source='+WDaSou+';Persist Security Info=False;Jet OLEDB:Database Password=""';
ConIni.Destroy;
Except
on E:exceptiondo
begin
MessageBox(Handle,Pchar(E.message),'错误',Mb_ok+Mb_IconError);
end;
end;

try
with datamdo
begin
Aceq.Connection:=ReadCon;
Aceq.Close;
Aceq.Sql.Clear;
Aceq.Sql.Add('Select cph From Cpinfo');
Aceq.Open;
Aceq.First;
Form1.ProgressBar1.Min:=1;
// if Aceq.RecordCount>0 then
//在线程中无法返回记录数
while not Aceq.eofdo
begin
cp:=Aceq.FieldByName('cph').AsString;
Oraq.Connection := OraDBCon;
Oraq.Close;
Oraq.SQL.Clear;
Oraq.SQL.Add('Select A1,A3,A7,A4,A5 From wf_new_one where A3='''+cp+'''');
Oraq.Open;
Oraq.First;
// if Oraq.RecordCount>0 then
while not Oraq.Eofdo
begin
sCfd:=Oraq.FieldByName('A1').AsString;
Cp:=Oraq.FieldByName('A3').AsString;
sJsz:=Oraq.FieldByName('A7').AsString;
sWzDate:=Oraq.FieldByName('A4').AsString;
sWzAdd:=Oraq.FieldByName('A5').AsString;
sName:='';
sSfz:='';
sWzCode:= '';
sFk:='';
sql1:=Format('insert into WzInfo (Cfd,Cph,Cxdh,xn,Sfz,Jsz,Wzdate,Wzadd,WzCode,Wzxw,Kf,Fk) ' +
'Values (''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'')',
[sCfd,Cp,Cxdh,sName,sSfz,sJsz,sWzDate,sWzAdd,sWzCode,Wz,sKf,sFk]);
ExecSql(sql1);
Oraq.Next;
end;
Oraq.Close;
towq.Connection:=OraDbCon;
towq.Close;
towq.SQL.Clear;
towq.SQL.Add('Select A2,A3,A5,A6,A7,A8,A9,A10 From wf_new_tow Where A5='''+cp+'''');
towq.Open;
towq.First;
// if towq.RecordCount>0 then
while not towq.Eofdo
begin
sSfz:=towq.FieldByName('A2').AsString;
sName:=towq.FieldByName('A3').AsString;
Cp:=towq.FieldByName('A5').AsString;
sWzDate:=towq.FieldbyName('A6').AsString;
sWzAdd:=towq.FieldByName('A7').AsString;
sWzCode:=towq.FieldByName('A8').AsString;
sFk:=towq.FieldByName('A9').AsString;
sJsz:=towq.FieldByName('A10').AsString;
sCfd:='';
Cxdh:='';
Wz:='';
sKf:='';
sql2:=Format('insert into WzInfo (Cfd,Cph,Cxdh,xn,Sfz,Jsz,Wzdate,Wzadd,WzCode,Wzxw,Kf,Fk) ' +
'Values (''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'')',
[sCfd,Cp,Cxdh,sName,sSfz,sJsz,sWzDate,sWzAdd,sWzCode,Wz,sKf,sFk]);
ExecSql(sql2);
towq.Next;
end;
towq.Close;
//一定要释放掉
//**************Read wf_new_three Date***********************************
threeq.Connection:=OraDbCon;
threeq.Close;
threeq.SQL.Clear;
threeq.SQL.Add('Select A1,A2,A4,A5,A6,A11,A12 From wf_new_three Where A5='''+cp+'''');
threeq.Open;
threeq.First;
// if threeq.RecordCount>0 then
while not threeq.Eofdo
begin
sName:=threeq.FieldByName('A1').AsString;
sSfz:=threeq.FieldByName('A2').AsString;
Cp:=threeq.FieldByName('A4').AsString;
sWzDate:=threeq.FieldByName('A5').AsString;
sWzCode:=threeq.fieldByName('A6').AsString;
sWzAdd:=threeq.FieldByName('A11').AsString;
sJsz:=threeq.FieldByName('A12').AsString;
sCfd:='';
Cxdh:='';
Wz:='';
sKf:='';
sFk:='';
sql3:=Format('insert into WzInfo (Cfd,Cph,Cxdh,xn,Sfz,Jsz,Wzdate,Wzadd,WzCode,Wzxw,Kf,Fk) ' +
'Values (''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'',''%s'')',
[sCfd,Cp,Cxdh,sName,sSfz,sJsz,sWzDate,sWzAdd,sWzCode,Wz,sKf,sFk]);
ExecSql(sql3);
threeq.Next;
end;
threeq.Close;
Aceq.Next;
Form1.ProgressBar1.StepIt;
end;
end;
Form1.Label1.Caption:='数据处理完成!';
Form1.ProgressBar1.Min:=0;
if Assigned(datam) then
FreeAndNil(datam);
except
on e :Exceptiondo
begin
ActiveX.CoUninitialize;
MessageBox(Handle,pchar(e.Message),'错误',Mb_ok+Mb_Iconerror);
end;
end;
end;
 
在线程初始的时候传入 Application的Handle,然后执行 Application.Terminate,但是这样是不安全的,最好的方法就是在线程结束时传个消息给主表单,然后让它去释放相关资源,结束程序
 
Application.Terminate就可以啦
 
线程.OnTerminate = xxx;
在xxx过程里面写上
Dec(线程数量);
if 线程数量<=0 then
Application.Terminate
当然,在添加一个线程时,要Inc(线程数量)
 
后退
顶部