我在一个窗体上写了一个线程,线程的作用是用来连接另外一个数据库。当数据库不存在时,我关闭窗体时,整个系统会卡死在哪里,主要原因是我在关闭窗体时,连接数据库的线程一直在运行。即使把 adoconnection 的操时时间设置为0,也要10几秒才能补捉到错误
下面是线程的代码:
{ TUpdateData }constructor TUpdateData.Create(Suspended: boolean; con1: TAdoConnection);
begin
inherited create(Suspended);
FreeOnTerminate:=true;
condetailNew:=con1;
end;procedure TUpdateData.Execute;
begin
repeat
try
CoInitialize(nil);
if connectionStr<>'' then
begin
condetailNew.Close;
condetailNew.ConnectionString:=ConnectionStr;
condetailNew.Open;
end;
Synchronize(showTitle);
except
end;
until UpdateData.Terminated;
end;procedure TUpdateData.showTitle;
begin
IsLinkData:=true;
UpdateData.suspend;
showmessage('³É¹¦');
end;
下面是线程的代码:
{ TUpdateData }constructor TUpdateData.Create(Suspended: boolean; con1: TAdoConnection);
begin
inherited create(Suspended);
FreeOnTerminate:=true;
condetailNew:=con1;
end;procedure TUpdateData.Execute;
begin
repeat
try
CoInitialize(nil);
if connectionStr<>'' then
begin
condetailNew.Close;
condetailNew.ConnectionString:=ConnectionStr;
condetailNew.Open;
end;
Synchronize(showTitle);
except
end;
until UpdateData.Terminated;
end;procedure TUpdateData.showTitle;
begin
IsLinkData:=true;
UpdateData.suspend;
showmessage('³É¹¦');
end;
解决方案 »
- 送分100分:STRTOINT的用法
- ini文件数据读取问题!
- ADOConnection连接问题
- ClientDataSet如何与一个动态创建的DataSetProvider关联起来?急!!!
- pos怎么用?谁能告诉我。给分!
- 请教高手!!很急!!!可以另外给分!!
- 济南大富翁注意了,愿意做兼职的请和我联系
- 关于PC机与单片机串口通信速度问题及线程资源占用过高..急
- delphi动态生成text
- 出 500 分 外加人民币 200 元购买一份源程序(代理服务器软件,要求能分别让各机在不同时间内限上网,最好有日志记录)1/x
- 请教各位一个基础问题,不要笑我。
- delphi 循环点击网页问题,第二次无法完成点击
begin
repeat
try
CoInitialize(nil);
if connectionStr <>'' then
begin
condetailNew.Close;
condetailNew.ConnectionString:=ConnectionStr;
condetailNew.Open;
end;
Synchronize(showTitle);
except
// 这个地方如果出错了,你也应该把线程释放掉或者 挂起.
end;
until UpdateData.Terminated;
end;
超时时间设置成5秒试试
AdoConnection最好是在线程内动态创建,传进来的con1还是在主进程内
condetailNew还是指向con1,连接时就会卡死主进程
另外连接的过程是不是改成这样while (not Terminted) and (not condetailNew.connected) then
begin
try
CoInitialize(nil);
if connectionStr <>'' then
begin
condetailNew.Close;
condetailNew.ConnectionString:=ConnectionStr;
condetailNew.Open;
end;
except end;
end;
如果我把AdoConnection在线程内动态创建,当连接成功时,我怎么样来操作连接的数据库呢
我之所以这样写,是因为我在主线程要操作我要连接的数据库
或者像你那样在Execute里连接数据库
把连接字符串传进去就行了