当我的程式正在运行一个FOR循环的时候,单击了关闭按钮,则程式会在执行关闭命令以后,继续执行未完成的FOR循环,这样就会造成读地址之类的错误,而且程式也无法关闭了.该怎么解决这种问题呢?急...

解决方案 »

  1.   

    测试:
    procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
     while true do
     begin
       application.ProcessMessages;
       if tag=1 then break;
     end;
    end;procedure TForm1.BitBtn2Click(Sender: TObject);
    begin
      tag:=1;
      close;
    end;
      

  2.   

    procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
     while true do
     begin
       application.ProcessMessages;
       if tag=1 then break;
     end;
    end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      tag:=1;
    end;
      

  3.   

    To: jinjazz(近身剪(N-P攻略)) :我的FOR循环如下,我想过用这个办法,在每个IF前面加上APPLICATION.PROCESSMESSAGE,可是这样的话,那么我的程式会不会变得很慢呢?  for i:=0 to ts.Count -1 do
        begin
          if ComportNo<>'' then
            Begin
              TempCom:=Comportno;
            end else
              begin
                TempCom:=reg.ReadString(ts.Strings [i]);
              end;
          Label7.Caption :='初始化串口...';      if not TCDMA1.ComPortInit(strtoint(copy(TempCom,4,length(TempCom)-3)),BaudRate) then
            begin
              //TCDMA1.ComPortInit(strtoint(copy(TempCom,4,length(TempCom)-3)),BaudRate);
              TCDMA1.ComPortFree ;
              Label7.Caption :='初始化串口失败';
              flag:=False;
              continue;
            end;      Label7.Caption :='检查串口通讯...';
          if not TCDMA1.AT_SendAT_WithCheck then
            begin
              TCDMA1.ComPortFree ;
              Label7.Caption :='串口通讯失败';
              flag:=False;
              continue;
            end;      Label7.Caption :='正在检查UIM卡...';
          Label7.Caption :=TCDMA1.ReadRUIMStatus('FX') ;
          if Label7.Caption<>'UIM卡状态正常' then
            begin
               Label7.Caption:=TCDMA1.ReadRUIMStatus('HT');
               if Label7.Caption<>'UIM卡状态正常' then
                 begin
                  TCDMA1.ComPortFree ;
                  flag:=False;
                  sleep(100);
                  continue;
                 end else
                   begin
                     ComportNo:=TempCom;
                     VersionFlag:='HT';
                   end;
            end else
              begin
                ComportNo:=TempCom;
                VersionFlag:='FX';
              end;
          if Label7.Caption <>'UIM卡状态正常' then
            begin
              TCDMA1.ComPortFree ;
              flag:=False;
              sleep(100);
              continue;
            end else
              begin
                openstatus:=true;
                flag:=true;
                break;
              end;
        end;
      

  4.   

    for i:=0 to ts.Count -1 do
        begin
    -----------------这里加一个就可以了,你加那么多干什么