我的的程序里面开了几十个子线程;我在主界面上点退出之前是先关闭子线程,然后用 close;但刚进去不久可以退出来,但进去运行一段时间后,就点退出没有反映,也不会出错,就是退不出;要去强行关闭才行可用分不多了,见谅。

解决方案 »

  1.   

    每个线程下断点,关闭的时候单步跟踪下close事件,应该是有线程有问题。
      

  2.   

    靠线程的teminate方法是结束不了线程的,需要代码完善一下的,
    在TThread的说明中是建议我们在执行体中不断地判断线程的teminated是否为真,如果为真,则退出执行体,以达到Terminated:=True的效果,否则,线程还是会执行完Execute才会退出线程.
      

  3.   

     TerminateProcess(GetCurrentProcess(),0);退出!
      

  4.   

    我的线程是和com口上的硬件通讯,然后把接收数据存数据库,并在主界面listview上显示出来;现在有个情况补充一下:就是在菜单中用showmodal打开一个子页面也退不出来;在主界面上点退出按钮,程序设计是有个密码提示框要showmodal的,这个密码框都没有显示出来
      

  5.   

    这位大侠的方向,应该是对的;
    因为我点退出时还没有到关子线程和退主程序的步骤,这是我退出按钮的代码
    procedure TFMMain.BExitClick(Sender: TObject);
    var Isspy:boolean;
        j:integer;
    begin
         application.ProcessMessages;
         if BPause.Tag=0 then
         begin
              IsSpy:=true;
              BPauseClick(Nil);
         end else
             IsSpy:=false;
         closePassWord:='';     application.ProcessMessages;
         PasswordDlg:=TPasswordDlg.create(self);
         try
            PasswordDlg.ShowModal;
         finally
                PasswordDlg.free;
         end;     if ClosePassWord <> '' then
            if ClosePassWord = LoginUserInfo.UserPassword then
               begin
                    DM.Qtemp.close;
                    DM.Qtemp.SQL.Text :='update UserLoginInfo ' +
                                 ' set LogoutTime = '+floattostr(myDSub+DBaseTime)+
                                 ' where  (LogoutTime IS NULL) and (WorkerID = '''+ LoginUserInfo.UserID+''')';
                    try
                       DM.Qtemp.ExecSQL ;                   DM.qtemp.Close;
                        DM.qtemp.SQL.Text:='select DISTINCT(ComID) from passState where Kind > 0 and (computer = '''+mycomputer+''' or computer = '''+LocalIP+''') '+sPassID+' order by ComID ';
                        DM.qtemp.Open;                    while not  DM.qtemp.eof do
                        begin
                             application.ProcessMessages;
                             Com_Thread[DM.qtemp.fieldbyname('ComID').AsInteger].Terminate;
                             application.ProcessMessages;
                             sleep(100);
                             DM.qtemp.next;
                        end;                   Spy_Thread.Terminate;
                      // CloseAllConnect();
                    except
                    end;                sleep(1000);                close;
               end else
               begin
                    messagedlg('ÃÜÂë´íÎó',mtwarning,[mbok],0);
                    if IsSpy then
                       BPauseClick(Nil);
               end;                //Setevent(Spy_event);
    end;这个是BPauseClick的代码procedure TFMMain.BPauseClick(Sender: TObject);
    begin
         if BPause.Tag=0 then
          begin
               application.ProcessMessages;
               BPause.Caption:='¼à ¿Ø(&P)';
               BPause.Tag:=1;
               Resetevent(Spy_event);
               Pauseed:=true;
               Setevent(Relay_event);
               application.ProcessMessages;
          end else
          begin
               application.ProcessMessages;
               BPause.Caption:='Ôݠͣ(&P)';
               BPause.Tag:=0;
               Setevent(Spy_event);
               Pauseed:=false;
               Resetevent(Relay_event);
          end;
    end;
      

  6.   

    同意楼上,关闭线程要有三个条件
    1.terminate;
    2.在execute程序中要运行到 terminated:=true的地方。(就是说,外部其他线程不能直接关闭在循环中的本线程。
    3.本线程还有一个属性freeonterminate :=true;