Search code examples
delphitimerprogress

Procedures in line with timer


I accustomed to using a ProgressBar and a Timer to do some progression queue / in line processes. I am looking for a replacement of TTimer and ProgressBar. This is what I am trying to say. Here is OnTimer event :

    procedure TForm1.tmr1Timer(Sender: TObject);
    begin
    ProgBar1.Position := ProgBar1.Position +1;
    if ProgBar1.Position = 10 then
    begin
    m1.Lines.Add('Progress 1 : Exec A') ;
     ExecAA(Sender);
    end;
    if ProgBar1.Position = 20 then
    begin
    m1.Lines.Add('Progress 2 : Exec B');
    ExecAB(Sender);
    end;
     if ProgBar1.Position = 30 then
    begin
    m1.Lines.Add('Progress 3 : Exec C');
    ExecAC(Sender);
    end;
// and so on, and so forth ...
    end;

Since I don't need to see the ProgressBar, for now I just hide this ProgressBar. I tried to use Timer and an integer to replace, but seems is not suitable.

 procedure TForm1.tmr1Timer(Sender: TObject);
    var
    xx : Integer;
    begin
    xx := xx + 1;
    if xx = 10 then
    .....

Is it possible to do in other way to have the same exact function without using any ProgressBar ?


Solution

  •  procedure TForm1.tmr1Timer(Sender: TObject);
     var
       xx : Integer;
     begin
       xx := xx + 1;
       if xx = 10 then
     .....
    

    The problem here is that xx is a local variable. You get a new instance of the local variable every time the timer is called. And to compound matters you are not initializing the local variable. The compiler should warn you of that. I trust you have hints and warnings enabled, and are heeding them.

    What you need is some persistent state. You need your integer value to live for longer than the duration of the timer. Instead of using a local variable, use a member of the class.