[Gambas-user] Breaking processes

Doriano Blengino doriano.blengino at ...1909...
Wed Nov 4 14:23:36 CET 2009


Dimitris Anogiatis ha scritto:
>  Hey M,
>
> to stop the execution of a for loop all you have to do is check for a
> condition then use break
>
> for example
>
>
> PUBLIC SUB FCopy()
> DIM i AS Integer
> Dim stopLoop as Boolean
> DIM filepaths AS String[]
> ........................' the array filepaths is filled with contents
> FOR i=0 To filepaths.Count-1
>
> if stopLoop = False THEN
>    SHELL "cp -f  " & filepaths[i] & " /home/destination"
> Else
>    BREAK
> ENDIF
> NEXT
>   
You don't say anything about using a button to stop the cycle.

To do so, you must put a button on the form. To let this button react to 
mouse clicks, you must use a WAIT inside the loop - without WAIT, the 
button gets the click, but delays its reaction until your current 
subroutine finishes - so you don't break/cancel anything.

Then, your button must do something your current loop can check. This is 
a global variable - it can not be one declared inside the subroutine, 
because nothing from the extern can interfere with local variables. The 
event handler for the cancel button can set this global variable, and 
the copy loop can check this variable. These information are not 100% 
accurate - but you get the point.

To summarize, you put a btCancel button on the form, and use the 
following code:

    private StopLoop as boolean   ' global shared variable

    public sub fcopy()
      ...
      ...
      StopLoop = false;   ' always set a value to variables...

     FOR i=0 To filepaths.Count-1

          WAIT   ' otherwise btCancel does not react to clicks in the
    correct time
          if StopLoop then BREAK   ' WHO set this variable? The next
    subroutine...
          SHELL ....
          ...

    end

    public sub btCancel_Click()
      StopLoop = true   ' clicking btCancel comes here and sets the
    global variable
    end

I didn't tested this code, but it should work.

Best regards,
Doriano

>> How can I stop the execution of a loop which includes shell command for
>> copying files?
>>
>> Example:
>> PUBLIC SUB FCopy()
>> DIM i AS Integer
>> DIM filepaths AS String[]
>> ........................' the array filepaths is filled with contents
>> FOR i=0 To filepaths.Count-1
>> SHELL "cp -f  " & filepaths[i] & " /home/destination"
>> NEXT
>>
>> END
>>
>> I'd like to add a Cancel button which would break the loop.Thanks!
>>
>>     




More information about the User mailing list