【问题标题】:Why does my marquee progress bar not animate?为什么我的选框进度条没有动画?
【发布时间】:2013-10-18 08:42:37
【问题描述】:

我正在做一个必须通过 tcp / ip 进行通信的应用程序。本程序有一个参数模态形式,获取服务器的IP,需要的数据,以及测试连接的测试按钮。 该测试按钮调用一个检查服务器是否处于活动状态的函数,我想显示一个带有典型进度条的表单,带有 pbstMarquee 样式,表明您正在尝试建立连接。 这是测试按钮的代码:

procedure TFormConfiguracion.ButtonTestClienteClick(Sender: TObject);
begin
   if TestTCPClient(EdIPCliente.Text, EdPasswordProtocolo.Text, EdPuertoCliente.Value,
   self) then  
   begin    
     MensajeInformacion('Conexión con el Servidor Establecida con Exito!',''); //Ok
   else
   begin    
     MensajeError('Error al Conectar con el Servidor!',''); //Error
   end;
 end;

函数TestTCP的代码:

function TestTCPClient(Host,Password: String; Puerto: Integer; AOwner: TComponent): 
Boolean;
var
  TCPCliente: TIdTCPClient;
  textoEnvio: String;
begin
  TCPCliente := TIdTCPClient.Create(nil);
  Result := False;
  TCPCliente.Host := Host;
  TCPCliente.Port := Puerto;
  TCPCliente.ConnectTimeout := 20000;
  textoEnvio := Trim(Password)+'|TEST|#';
  try
    ShowFormCompConexion(AOwner, 'Intentando establecer conexión con el equipo   
    '+Host+'...'); //Trying to connect
    TCPCliente.Connect;
    TCPCliente.Socket.ReadTimeout := 10000;
    TCPCliente.Socket.WriteLn(textoEnvio, TEncoding.ANSI);
    if (TCPCliente.Socket.ReadLn(TEncoding.ANSI) = 'OK#') then
      Result := True;
    CloseFormCompConexion;
 except
   on E : Exception do
   begin
     CloseFormCompConexion;
     Exit;
    end;

  end;
end;

以及显示带有进度条的表单的函数代码:

procedure ShowFormCompConexion(AOwner: TComponent; Dato: String);
begin
  Form_CompConexion := TFormCompConexion.Create(AOwner);
  Form_CompConexion.LbDato.Caption := Dato;
  Form_CompConexion.Show;
  Form_CompConexion.Repaint;
end;

问题是这个表格一直处于非活动状态,我的意思是不移动进度条,就像等待她完成这个过程一样。我试着放一个 gif,但没有人让我编辑 Tedit ....

对不起我的英语

【问题讨论】:

    标签: delphi


    【解决方案1】:

    进度条需要 GUI 线程来服务其消息队列。进度条不刷新的事实表明 GUI 线程没有为其队列提供服务。

    队列未得到服务的原因是 GUI 线程忙于阻塞套接字通信。由于 Indy 使用阻塞协议,所以只要您从 GUI 线程中使用 Indy,就根本无法解决这个问题。

    解决方案?将您的阻塞通信放在不同的线程中。这允许您为 GUI 线程的消息队列提供服务。

    【讨论】:

    • 我该怎么做?
    • 使用线程的替代方法是使用 Indy 的 TIdAntiFreeze 组件,它允许在套接字阻塞 UI 时继续处理消息队列。
    • @RemyLebeau 它是如何工作的?我以为 Indy 使用的底层套接字 API 是阻塞的?
    • @DavidHeffernan:Indy 确实使用阻塞套接字。但是当在主线程中使用时,Indy 的阻塞操作也会周期性地调用TIdAntiFreeze,如果它存在,那么它可以泵送消息队列。不是最好的解决方案,但它是一个可用的选项,适用于不流利/不习惯多线程编程的人,或者只是不想使用线程的人。
    猜你喜欢
    • 2017-07-21
    • 2022-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    相关资源
    最近更新 更多