Topic: a way to stop a script with an if condition?

hello there. i have a if that does cancel = true, but it doesn't stop the rest of the script from running
Can i use a comand to stop the rest of the script from runing?

Re: a way to stop a script with an if condition?

Use "Exit;"

Re: a way to stop a script with an if condition?

but will exit close my form?

4 (edited by mathmathou 2017-12-06 19:40:19)

Re: a way to stop a script with an if condition?

Hello Vasco,


Stoping à procedure while it is running is not very difficult, but your approach will depend on the type of procedure.


If for example this is a simple procedure waiting for a user input for example, and the edit field has not been filled before the button click, you can use something like :

If length(Form2.Edit1.Text) = 0 then
     Begin
          Cancel := True;
          Showmessage(sorry you left the field empty);
          Form2.Close;
     End;

If the procedure is looping through calculations or repeating tasks and you want to stop it on a condition ? You could use Exit or Break like ehwagner suggested but make sure you condition is tested each cycle or it will not stop. And yes, if the procedure exits, you can issue a Close command.


Now, the case I have been faced many times is as follows : one of my application has very long procedures testing thousands of records in a very long loop. The user is informed of the progress of the operation whith a progress bar but sometimes he might want to stop the process to resume it sometimes later.

     if condition = False then
          begin
               Break;
           end
     else if condition = True then...

In that case, my approach is different :
- I create a global Boolean variable (because I use it in many places)
- I set this variable to true at the beginning of the procedure
- as long as the cancel button is not clicked the procedure runs
- if the Cancel button is clicked, the procedure stops and after a message the form is closed


This looks like :

running := True;
for i := 0 to online_count - 1 do
     begin
          if running = False then
               begin
                    Break;
               end
          else if running = True then
               begin
                   .......

Then the cancel button is associated with a simple procedure like :

procedure BtCancel_OnClick (Sender: string; var Cancel: boolean);
begin
    running := False;
end;

But the procedure will not know if the button has been clicke dor not because it uses all the cycles and does not get messages from the "outside", the user interface is kind of frozen.
So, to make sure the user can interact with the interface, I had IN THE LOOP (very important) an instruvction that tells the procedure to "listen to input if any" with :

Application.ProcessMessages;

The skeleton of the code is :

running := True;

for i := 0 to online_count - 1 do
     begin
          if running = False then
               begin
                    Break; //but you cna do what you what you want here
               end
          else if running = True then
               begin
                   .......
               end;
          Application.ProcessMessages; //this is tested each cycle of the for i := 0 to some value and will detect a click on the cancel button
     end;

Hope this helped


Cheers



Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

Re: a way to stop a script with an if condition?

VascoMorais wrote:

but will exit close my form?

No.

The exit procedure abruptly terminates the current function or procedure.

Dmitry.

Re: a way to stop a script with an if condition?

blackpearl8534 wrote:

How to Import Data From SQL Dat to our sqlite.db

You can use free tool SQLite Studio for that. https://sqlitestudio.pl

Dmitry.