Leave function running in loop until application close

1

I have the following code that changes image from time to time.

if Timer1.enabled = true then
begin
 Gauge1.Progress := Gauge1.Progress +1;
   if Gauge1.Progress=10 then
Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg');

   if Gauge1.Progress=30 then
Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg');

   if Gauge1.Progress=50 then
Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg');
 end;

How do I loop around until the application is finished

    
asked by anonymous 06.06.2016 / 14:24

4 answers

1

Let us clarify your doubt ... You can do it in the following way. Since you have already included a Timer component on the screen called Timer1.

1st - Create an OnTimer event of the Timer component.

2º - Put the code, it looks like this.

procedure TForm1.Timer1Timer(Sender: TObject);
begin    
    Gauge1.Progress := Gauge1.Progress + 1;

    if Gauge1.Progress = 10 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg')    
    else if Gauge1.Progress = 30 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg')    
    else if Gauge1.Progress = 50 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg');    
end;

3º - Define the time in which Gauge1.Progress will be incremented, in Timer1 in the Interval property, remembering that the time is measured in ms, that is, 1000 is 1 second. If it is 1 second the set time, in 10 seconds the process will change the image to 2.jpg, in 30 seconds 3.jpg and so on.

4º - When you want to start this count, simply put the Enabled property of Timer1 in True, and otherwise False.

5º - In this case from 50 the image will always be 1.jpg, let's say that when the Gauge1.Progress reaches 60 we will want to start counting again from 0. Then we change the code and put something like this:

procedure TForm1.Timer1Timer(Sender: TObject);
begin    
    Gauge1.Progress := Gauge1.Progress + 1;

    if Gauge1.Progress = 10 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg')    
    else if Gauge1.Progress = 30 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg')    
    else if Gauge1.Progress = 50 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg')
    else if Gauge1.Progress = 60 then
        Gauge1.Progress = 0;     
end;

6º - If Gauge1.Progress can not be zeroed, depending on the logic of your program, then create a variable in the Form of type Integer to do this process. Getting something like:

private 
    contador: Integer;
...
...
procedure TForm1.Timer1Timer(Sender: TObject);
begin    
    Gauge1.Progress := Gauge1.Progress + 1;
    Inc(contador);

    if contador = 10 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg')    
    else if contador = 30 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg')    
    else if contador = 50 then
        Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg')
    else if contador = 60 then
        contador = 0;
end;
    
07.06.2016 / 09:38
1

Use a repeat structure for this, let's try with while :

while Gauge1.Progress < 100 do
begin
  if (Gauge1.Progress = 10) then
  begin
    Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg');
  end
  else if (Gauge1.Progress = 30) then
  begin
    Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg');
  end
  else if (Gauge1.Progress = 50) then
  begin
    Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg');
  end;
  Inc(Gauge1.Progress);
end;

In other words, as long as% does not reach 100%, you will be updating, now you need to fit this structure so that you do not stop your system to run it! This already depends on when you will run this implementation of Gauge .

    
06.06.2016 / 19:39
0

Dude, basically you can use the TTimmer, put in his event: onTimer, and set the Interval for as long as you need it.

    
06.06.2016 / 14:30
0
procedure TForm1.Timer1Timer(Sender: TObject);
begin
    if Timer1.enabled = true then
    begin
        Gauge1.Progress := Gauge1.Progress +1;
        if Gauge1.Progress=10 then
            Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg');

        if Gauge1.Progress=30 then
            Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg');

        if Gauge1.Progress=50 then
            Image1.Picture.LoadFromFile('C:\contas_a_pagar\img\home.jpg');
    end;
end;

In the Interval property you set the loop time in milliseconds. Timer1.interval: = 1000; // 1 second

    
06.06.2016 / 15:51