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.

[sourcecode='delphi']
{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;
[/sourcecode]

Implementamos el constructor...

[sourcecode='delphi']
{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;
[/sourcecode]

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

[sourcecode='delphi']
procedure TContador.Inicio(Sender :TObject);
begin
Self.Acum:=0;
DebugLn('Inicio');
end;
[/sourcecode]

Aqui simplemente queria saber cuando se lanza OnStopTimer ;)

[sourcecode='delphi']
procedure TContador.Fin(Sender :TObject);
begin
DebugLn('Fin');
end;
[/sourcecode]

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

[sourcecode='delphi']
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;
[/sourcecode]

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

Tags

  • fpclazarus

Add new comment

Filtered HTML

  • Web page addresses and email addresses turn into links automatically.
  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
CAPTCHA
3 + 2 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.