miniTip : Extendiendo el TIdleTimer de Lazarus

Escenario: Se requiere lanzar un proceso cada cierto tiempo que la aplicacion este sin hacer nada, o sea, en Idle :)

Como TIdleTimer no tiene “memoria” creamos nuestra clase TContador ( en honor a nuestro campeon ;P ) dotandole de memoria.

{TContador}
TContador = class(TIdleTimer)
procedure Contando(Sender :TObject);
procedure Inicio(Sender :TObject);
procedure Fin(Sender :TObject);
private
  Acum, tMax : Integer;
public
  constructor Create(AOwner : TComponent ;Maximo : Integer; curr : TCurrante);
end;

Implementamos el constructor…

{TContador}
constructor TContador.Create(AOwner : TComponent; Maximo : Integer);
begin
  inherited Create(AOwner);
  Self.Enabled:=false;
  Self.OnStartTimer:=@Inicio;
  Self.OnStopTimer:=@Fin;
  Self.OnTimer:=@Contando;
  Self.Acum:=0;
  Self.tMax:=Maximo;
  Self.AutoEnabled:=true;
end;

Cada ver que se produzca el evento OnStartTimer pondremos el acumulador a 0

procedure TContador.Inicio(Sender :TObject);
begin
  Self.Acum:=0;
  DebugLn('Inicio');
end;

Aqui simplemente queria saber cuando se lanza OnStopTimer ;)

procedure TContador.Fin(Sender :TObject);
begin
  DebugLn('Fin');
end;

Este proceso se ejecuta cuando la aplicacion esta Idle y lanza el proceso cuando el tiempo acumulado supera el tiempo maximo.

procedure TContador.Contando(Sender :TObject);
begin
  Inc(self.Acum);
  if self.Acum > self.tMax then
    begin
      Self.Enabled:=false;
      Self.Enabled:=true;
      DebugLn('Lanzamos proceso');
    end;
  DebugLn('Contando - ' + IntToStr(self.acum));
end;

Proceso sencillo aunque cuando eres un newbie no aparece en la mente de manera inmediata

Share on Twitter

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos necesarios están marcados *

*

Puedes usar las siguientes etiquetas y atributos HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Spam protection by WP Captcha-Free