我在一个窗体上写了一个线程,线程的作用是用来连接另外一个数据库。当数据库不存在时,我关闭窗体时,整个系统会卡死在哪里,主要原因是我在关闭窗体时,连接数据库的线程一直在运行。即使把 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;
解决方案 »
- 调用LPT的问题
- 如何将form1中定义的类在form2中使用?
- 求购图书
- 恭喜月亮5月2日起合法同居
- 怎样用QuickReport做这样的报表?
- 大菜鸟的小问题---如何在窗体上显示文字,不用label
- 在delphi中有什麼辦法能把jpg圖片導入excel指定單元格中急。。。我只有這麼多了。。
- mdi和cli的窗体DLL调用系列问题~请教了,小弟献上100分,不够再给
- 如何在窗体透明的前提下,使image中的位图的背景也透明?急!!!!
- 普通数据库系统能通过 odbc 或 bde 指定数据源, 现在我用 ado 如果不通过 odbc 有什么办法在安装时指定数据源?(好像通过odbc 会降低效 )
- 请教各位一个基础问题,不要笑我。
- 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里连接数据库
把连接字符串传进去就行了