To continue if true

Asked

Viewed 44 times

2

Well, I’m trying to run several downloads, one after the other, and I’d like you to start the next one after the other. The codes are these:

  if StrToInt(version) < StrToInt(version2) then
  begin
    for x := StrToInt(version) to StrToInt(version2) do
      url := 'http://pokestage.ddns.net/patch/'+IntToStr(x)+'.rar';
    BaixarArquivo(IdHTTP1, url, x);
  end
  else
    sProgressBar1.Position := sProgressBar1.Max;
  slabel1.caption := '100%';
end;

Where Downloadfile returns true when the file finishes downloading. How do I only go to the next one after one is completed?

  • Yes, it is running normally, only it already starts by downloading the last file.

1 answer

2


As per your comment:

Yes, it is running normally, only it already starts by downloading the last file.

Your problem is in your command for.
It’s a matter of language syntax Pascal, the Begin and the End.
Like the { } for php among others.

Therefore of identation be an important thing.
Take your example:

  if StrToInt(version) < StrToInt(version2) then
  begin
    // o for executa todos os contadores..
    for x := StrToInt(version) to StrToInt(version2) do
      url := 'http://pokestage.ddns.net/patch/'+IntToStr(x)+'.rar'; 

    // quando chegar aqui, a URL será a última --> valor de version2
    BaixarArquivo(IdHTTP1, url, x);
  end
  else
    sProgressBar1.Position := sProgressBar1.Max;
  slabel1.caption := '100%';
end; // <-- sobrando, a princípio

How this bond should be for?

for x := StrToInt(version) to StrToInt(version2) do
begin
  url := 'http://pokestage.ddns.net/patch/'+IntToStr(x)+'.rar'; 
  BaixarArquivo(IdHTTP1, url, x);
end;

Now it generates a url and passes to the Download method, then generates another one, and then goes to the Download method. So, successively.

How the lines should look:

if StrToInt(version) < StrToInt(version2) then
begin
  for x := StrToInt(version) to StrToInt(version2) do
  begin
    url := 'http://pokestage.ddns.net/patch/'+IntToStr(x)+'.rar'; 
    BaixarArquivo(IdHTTP1, url, x);
  end;
end
else
  sProgressBar1.Position := sProgressBar1.Max;

slabel1.caption := '100%';

On the commands within the method OnTimer of the component TTimer:

There’s a detail here you need to know.

The Ttimer component will run the Ontimer event from time to time as set in its Interval property.
Therefore, as desired by you in your last question, it is fundamental in the first line of the method OnTimer be assigned the value false for the component’s Enable method property TTimer.

procedure Form1.Timer1OnTimer(sender: TObject);
begin
  Timer1.Enable := false;

  ... // restante dos seus comandos.
end;

If you do not do this, the Ontimer method will be run repeatedly, trying to do the download commands again.
At the given time, you can have the download commands run again even before you have finished the first iteration of for with the method BaixarArquivo.

I hope that’s made clear.

  • Perfect, problem solved.

  • 1

    I had already done it, it was clear yes.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.