我在一个窗体上写了一个线程,线程的作用是用来连接另外一个数据库。当数据库不存在时,我关闭窗体时,整个系统会卡死在哪里,主要原因是我在关闭窗体时,连接数据库的线程一直在运行。即使把 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;

解决方案 »

  1.   

    连接你要的数据库之前先连接到master判断有没有该数据库,没有就不要连接
      

  2.   

    关闭的时候,先让线程释放掉;UpdateData.Terminate;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; 
     
      

  3.   

    他说的是连接错误的服务器地址或者网络不通的情况
    超时时间设置成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;
      

  4.   

    to  wintergoes:
       如果我把AdoConnection在线程内动态创建,当连接成功时,我怎么样来操作连接的数据库呢
    我之所以这样写,是因为我在主线程要操作我要连接的数据库
      

  5.   

    创建线程的时候挂起,调用另外一个public过程连接数据库
    或者像你那样在Execute里连接数据库
    把连接字符串传进去就行了
      

  6.   

    应该是连接出错,ADO连接不到就会停那里一段时间
      

  7.   

    在窗体关闭时,释放线程UpdateData.Terminate;