+ -
当前位置:首页 → 问答吧 → 为什么连成功后Timer1控件就不在循环了

为什么连成功后Timer1控件就不在循环了

时间:2011-09-20

来源:互联网

Delphi(Pascal) code

procedure TForm1.Timer1Timer(Sender: TObject);
begin
IdTCPClient1.Host:=192.168.1.88;
IdTCPClient1.Port:=5555;        
 if not IdTCPClient1.Connected then begin
  try
      IdTCPClient1.Connect;      
      IdTCPClient1.Writeln(ip_w);      
      StatusBar1.Panels.Items[0].Text:='连接成功!';
  except
      StatusBar1.Panels.Items[0].Text:='连接错误!';
      exit;
  end;
 end;

end;



为什么连成功后Timer1控件就不在循环了

作者: nydns   发布时间: 2011-09-20

难道其他地方有
Timer1.Enabled:=False;????

作者: m617105   发布时间: 2011-09-20

你怎样看出它不循环了?

作者: babydog01   发布时间: 2011-09-20

if not IdTCPClient1.Connected then begin
你的这条语句已经跳过下面的过程了。

作者: babydog01   发布时间: 2011-09-20

楼上的回答正确,楼主若不相信Timer1Timer事件还在执行,可以在if not IdTCPClient1.Connected then begin语句设置断点,它一定会运行到这句停下。

作者: gzzai   发布时间: 2011-09-20

楼主的代码应该改为这样:
Delphi(Pascal) code
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  IdTCPClient1.Host:=192.168.1.88;
  IdTCPClient1.Port:=5555;        
  if not IdTCPClient1.Connected then begin
  try
    IdTCPClient1.Connect;      
    IdTCPClient1.Writeln(ip_w);      
    StatusBar1.Panels.Items[0].Text:='连接成功!';
    Timer1.Enabled:=False;
  except
    StatusBar1.Panels.Items[0].Text:='连接错误!';
    exit;
  end;
end;



作者: gzzai   发布时间: 2011-09-20

如果没有time.enabled := false,那这个timer一直会执行.
你这个是因为你的代码有判断连接成功与否的标志:
if not IdTCPClient1.Connected then begin

作者: xiaxia421   发布时间: 2011-09-20