again: How can I stop a loop?

pdabasic

Active Member
Licensed User
Hy!

I wrote a little image merger script, but when i run it I saw, I get a wrong destination folder. I tried to stop the debuging and click to close button but for 10 seconds couldn't close the program.

First: why couldn't stop the debugging when i clicked the stop button?

Second: How can I implement a cancel method in this script?

B4X:
Sub App_Start
   frmMain.Show
   ctrlExProgBar.New1("frmMain",0,200,240,25)
   ctrlExProgBar.Minimum=1
   ctrlExProgBar.Maximum=804
   bmpExMerge.New2(2048,1237)
   rectEx1.New1(0,0,1024,1237)
   rectEx2.New1(1024,0,1024,1237)
   drawEx1.New2(bmpExMerge.Value)
End Sub

Sub btnStart_Click
   x=1
   For i=1 To 804
      bmpEx1.New1(AppPath&"\images\" & i &".jpg")
      bmpEx2.New1(AppPath&"\images\" & i+1 &".jpg")
      drawEx1.DrawImage(bmpEx1.Value,rectEx1.Value,rectEx1.Value,False)
      drawEx1.DrawImage(bmpEx2.Value,rectEx1.Value,rectEx2.Value,False)
      bmpExMerge.SaveImage(AppPath&"\merged\"&x&".jpg","J")
      i=i+1
      x=x+1
      ctrlExProgBar.Value=i
   Next
   Msgbox("Finish merging")
End Sub

Thank you for help
 

derez

Expert
Licensed User
Longtime User
If you put "DoEvents" inside the loop the program will look each cycle for events, for example if you add in the loop
B4X:
...
doevents
if flag then exit
...
then changing the flag to true (by a button_click sub for example) will cause exit from the loop.
This, however, slows the loop implementation time.
 

derez

Expert
Licensed User
Longtime User
No,
B4X:
For i=1 To 804
        bmpEx1.New1(AppPath&"\images\" & i &".jpg")
        bmpEx2.New1(AppPath&"\images\" & i+1 &".jpg")
        drawEx1.DrawImage(bmpEx1.Value,rectEx1.Value,rectEx1.Value,False)
        drawEx1.DrawImage(bmpEx2.Value,rectEx1.Value,rectEx2.Value,False)
        bmpExMerge.SaveImage(AppPath&"\merged\"&x&".jpg","J")
        i=i+1
        x=x+1
        ctrlExProgBar.Value=i
        doevents
        if flag then exit
Next
 

pdabasic

Active Member
Licensed User
I can change the flag until the for method is running?

B4X:
Sub btnCancel_Click
 flag=false
End Sub
 

manios

Active Member
Licensed User
Longtime User
You must set Flag=True with the Cancel Button to exit the loop!
 
Top